找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 973|回复: 0
打印 上一主题 下一主题

phpmailer绑定邮箱的实现方法

[复制链接]

2487

主题

2487

帖子

7391

积分

论坛元老

Rank: 8Rank: 8

积分
7391
跳转到指定楼层
楼主
发表于 2018-2-14 05:29:59 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式

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


1.配置
'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}您好:
    您的激活码为
{$code}
,请将激活码输入进行验证! 激活码有效期为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->assword = C('email_password'); // 邮局密码
    $mail->ort=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 = "尊敬的用户{$username}您好:
    您的激活码为
{$code}
,请将激活码输入进行验证! 激活码有效期为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程序设计有所帮助。
            
            
您可能感兴趣的文章:
  • phpmailer在服务器上不能正常发送邮件的解决办法
  • phpmailer发送邮件之后,返回收件人是否阅读了邮件的方法
  • PHP邮件发送类PHPMailer用法实例详解
  • 使用PHPMailer实现邮件发送代码分享
  • thinkphp使用phpmailer发送邮件的方法
  • PHP借助phpmailer发送邮件
  • 汇总PHPmailer群发Gmail的常见问题
  • phplist及phpmailer(组合使用)通过gmail发送邮件的配置方法
  • phpmailer简单发送邮件的方法(附phpmailer源码下载)
  • php使用phpmailer发送邮件实例解析
  • PHPMailer发送邮件
  • 使用PHPMailer发送邮件实例
  • PHP插件PHPMailer发送邮件功能
  • Linux服务器下PHPMailer发送邮件失败的问题解决
  • PHPMailer使用QQ邮箱实现邮件发送功能
  • phpmailer发送邮件功能
  • ThinkPHP3.2利用QQ邮箱/163邮箱通过PHPMailer发送邮件的方法
  • 实例分析PHP中PHPMailer发邮件
            
  • 分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
    收藏收藏
    回复

    使用道具 举报

    您需要登录后才可以回帖 登录 | 立即注册

    本版积分规则

    用户反馈
    客户端