发布于 2017-08-30 05:16:05 | 153 次阅读 | 评论: 0 | 来源: 网友投递

这里有新鲜出炉的PHP设计模式,程序狗速度看过来!

PHP开源脚本语言

PHP(外文名: Hypertext Preprocessor,中文名:“超文本预处理器”)是一种通用开源脚本语言。语法吸收了C语言、Java和Perl的特点,入门门槛较低,易于学习,使用广泛,主要适用于Web开发领域。PHP的文件后缀名为php。


这篇文章主要介绍了phpmailer绑定邮箱的实现方法,结合实例形式较为详细的分析了phpmailer绑定邮箱的配置、功能实现与相关操作技巧,需要的朋友可以参考下

本文实例讲述了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程序设计有所帮助。



最新网友评论  共有(0)条评论 发布评论 返回顶部

Copyright © 2007-2017 PHPERZ.COM All Rights Reserved   冀ICP备14009818号  版权声明  广告服务