phpmailer绑定邮箱的实现方法

 更新时间:2016年12月2日 10:11  点击:1693

本文实例讲述了phpmailer绑定邮箱的实现方法。分享给大家供大家参考,具体如下:

效果如下:

1.配置

<?php
return array (
 'email_host' => 'smtp.aliyun.com',
 'email_port' => '25',
 'email_username' => 'diandodo@aliyun.com',
 'email_password' => 'xxxxxx',
 'email_from' => 'diandodo@aliyun.com',
 'email_fromname' => '点多多',
 'email_subject' => '助店宝商户激活邮箱',
 'email_body' => "尊敬的用户{$username}您好:
    您的激活码为<font color='red'>{$code}</font>,请将激活码输入进行验证! 激活码有效期为6分钟^_^",
);

2.发送函数

// 发送邮件
private function _sendEmail($email,$code,$username = '') {
    import('@.ORG.phpmailer');
    $mail = new PHPMailer(); //建立邮件发送类,类名不一定与引入的文件名相同
    $mail->CharSet = "UTF-8";
    $mail->IsSMTP(); // 使用SMTP方式发送
    $mail->Host = C('email_host'); // 您的企业邮局域名
    $mail->SMTPAuth = true; // 启用SMTP验证功能
    $mail->Username = C('email_username'); // 邮局用户名(请填写完整的email地址)
    $mail->Password = C('email_password'); // 邮局密码
    $mail->Port=C('email_port');
    $mail->From = C('email_from'); //邮件发送者email地址
    $mail->FromName = C('email_fromname');
    $mail->AddAddress("$email", "$username");
    $mail->IsHTML(true); // set email format to HTML //是否使用HTML格式
    $mail->Subject = C('email_subject'); //邮件标题
    $email_body = "尊敬的用户<strong>{$username}</strong>您好:
    您的激活码为<font color='red'>{$code}</font>,请将激活码输入进行验证! 激活码有效期为6分钟^_^";
    $mail->Body = $email_body; //邮件内容,上面设置HTML,则可以是HTML
    if(!$mail->Send())
    {
      return array('status'=>2,'info'=>$mail->ErrorInfo);
    } else {
      return array('status'=>1,'info'=>'发送成功');;
    }
}

3.生成验证码保存到session中,并发送

// 发送邮箱激活码
public function sendActivationcode() {
    session($this->activationtime, null);
    $activationtime = session($this->activationtime);
    $email = $this->_post('email', 'trim');
    if (IS_AJAX && (!$activationtime || time() > $activationtime)) {
      $activationcode = rand(1000, 9999);
      $res = $this->_sendEmail($email,$activationcode,$this->user['username']);
      if($res['status'] == 1) {
        //设置发送限制时间
        session($this->activationtime, time() + 50);
        session($this->activationcode, array('code' => $activationcode, 'time' => time() + 600));
        $this->ajaxReturn(array('result' => true));
      } else {
        //发送失败写入日志文件
        $log = date('Y-m-d H:i:s') . " 发送失败:{$res['info']}" . PHP_EOL;
        file_put_contents(RUNTIME_PATH . 'Log/activationcode.log', $log, FILE_APPEND);
        $this->ajaxReturn(array('result' => false, 'error' => $res['info']));
      }
    } else {
      $this->ajaxReturn(array('result' => false, 'error' => '错误的请求'));
    }
}

4.验证并绑定

// 绑定邮箱
public function bind_email() {
    if (IS_POST) {
      // 获取验证码
      $activationcode = $this->_post('activationcode','trim');
      $email = $this->_post('email','trim');
      $session_activationcode = session($this->activationcode);
      if (time() > $session_activationcode['time'] || $activationcode != $session_activationcode['code']) {
        $this->error('验证码有误');
      } else {
        M('User')->where(array('id'=>$this->user['id']))->save(array('email'=>$email));
        $this->success('绑定成功',U('Account/my'));
      }
    } else {
      $this->display();
    }
}

小结:

1. 这是一种思路,跟发送手机验证码差不多。
2. 区别在于一个是发送短信,一个是发送邮件。
3. 二一个,一个发送主体是阿里大鱼,一个发送主体是公司申请的邮箱。
4. 三一个,发送短信收费,发送邮件免费。

更多关于PHP相关内容感兴趣的读者可查看本站专题:《PHP网络编程技巧总结》、《PHP基本语法入门教程》、《PHP数组(Array)操作技巧大全》、《php字符串(string)用法总结》、《PHP运算与运算符用法总结》、《php面向对象程序设计入门教程》、《php+mysql数据库操作入门教程》及《php常见数据库操作技巧汇总》

希望本文所述对大家PHP程序设计有所帮助。

[!--infotagslink--]

相关文章

  • phpMailer 发送邮件

    //原创:www.111cn.net 注明:转载说明来处www.111cn.net // 昨天听一网友说用php 里面的mail发邮件发不出去,我想一般都是发不了的,现在大多数据邮件提供商都不准那样了...2016-11-25
  • 解决PHPMailer错误SMTP Error: Could not connect to SMTP host的办法

    PHPMailer发邮件时提示SMTP Error: Could not connect to SMTP host错误是smtp服务器的问题我们一起来看看关于SMTP Error: Could not connect to SMTP host问题的解...2016-11-25
  • Yii框架实现邮箱激活的方法【数字签名】

    这篇文章主要介绍了Yii框架实现邮箱激活的方法,基于邮箱发送邮件实现数字签名的激活功能,需要的朋友可以参考下...2016-10-20
  • phpmailer发送邮件 SMTP Error: Could not authenticate 错误

    今天在使用phpmailer发送邮件时居然提示SMTP Error: Could not authenticate,这个感觉是smtp设置的问题,下面我在网上找到了几种解决办法。 今天在使用phpmailer发...2016-11-25
  • phpmailer 发送邮件实例代码

    header("Content-type:text/html;charset=utf-8"); include('phpmailer/class.phpmailer.php'); include('phpmailer/class.smtp.php'); $mail = new PHPMailer();...2016-11-25
  • phpmailer邮件发送实例(163邮箱 126邮箱 yahoo邮箱)

    phpmailer是一个非常优秀的php邮箱发送插件了,他可以几乎实现任何邮箱登录发送,下面我介绍163邮箱 126邮箱 yahoo邮箱的发送方法。 准备工作: 我们必须注册一个邮...2016-11-25
  • 利用phpmailer 发送邮件代码[发送html内容]

    我们利用 phpmailer功能实现 邮件发送功能哦,这里还利用了模板呢,就是读取指定文件内容再发送给朋友。 <?php @session_start(); include(dirname(__FILE__).'....2016-11-25
  • 使用PHPMailer发送邮件实例代码总结

    PHPMailer发送邮件现在php开发者比较常用的一个邮件发送组件了,利用它我们几乎不需要考虑任何问题,只要简单的把代码放网上把邮箱用户名密码与stmp改一下就可以发邮件了...2016-11-25
  • PHP邮箱验证示例教程

    这篇文章主要为大家详细介绍了PHP邮箱验证示例,通过实例一步步带领大家认识PHP邮箱验证的过程,感兴趣的小伙伴们可以参考一下...2016-06-12
  • 在js中实现邮箱格式的验证方法(推荐)

    下面小编就为大家带来一篇在js中实现邮箱格式的验证方法(推荐)。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧...2016-10-25
  • phpmailer发送邮件实例程序

    在php中发邮件会用到mail函数 ,但是大多数情情况mail函数是不可用的,我们有插件来实例了phpmailer来实现发送邮件。 代码如下 复制代码 requi...2016-11-25
  • php 利用phpmailer 在线发送邮件

    html代码 <form action="lead_mail_send.php" method="Post" enctype="multipart/form-data" name="myfm"> <table width="80%" border="0" align="center" cellspa...2016-11-25
  • thinkphp中怎么使用phpmailer发送邮件

    phpmailer发送邮件是php开发者首选的一个邮件发送插件了,下面我来介绍怎么集成phpmailer到thinkphp框架了,有需要了解的朋友可参考。 phpmailer发送邮件功能很强...2016-11-25
  • phpmailer多邮件发送

    phpmailer多邮件发送 require("class.phpmailer.php"); $mail = new PHPMailer(); $mail->From = "list@mydomain.com"; $mail->FromName = "List manager"; $ma...2016-11-25
  • PHPMailer邮件类邮件发送

    本款邮件发送功能我们是用了国外一个开源码的邮件类,大家都可能用过的PHPMailer邮件类很简单,今天来讲一下简单的使用教程,有需要的朋友可以参考下,同时像其它的附件什么...2016-11-25
  • PHPMailer 发送邮件实例程序

    首先,先到http://phpmailer.sourceforge.net/这个地址去下载最新的PHPMailer的包(PHPMailer方法必须使用这个包)。   下载完成后解压到相应的目录。可以看到解压后的...2016-11-25
  • ThinkPHP 利用 PHPMailer 实现邮件发送

    本文章介绍了关于在thinkphp中利用了phpmailer来实现邮件发送的详细教程,有需要的朋友可以参考一下。 本文所使用的是ThinkPHP 2.1版和 PHPMailer 5.1版。(后者建议...2016-11-25
  • Yii统计不同类型邮箱数量的方法

    这篇文章主要介绍了Yii统计不同类型邮箱数量的方法,涉及Yii数据库查询及字符串的遍历、截取与判断相关操作技巧,需要的朋友可以参考下...2016-10-20
  • 使用PHPMailer发送邮件实例

    小编推荐的这篇文章介绍了使用PHPMailer发送邮件实例,非常实用,有兴趣的同学快来看看吧。 PHPMailer类源代码下载地址:https://github.com/PHPMailer/PHPMailerhttps...2017-07-06
  • phpmailer中文乱码与标题中文乱码解决

    本文章总结了关于在phpmailer发送邮件时出现的中文乱码与标题中文乱码解决方法总结,有需要的朋友可参考本文章。 一个客户的PHP网站需要一个邮件订阅功能,我采用一...2016-11-25