服务器之家:专注于服务器技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|JAVA教程|ASP教程|编程技术|正则表达式|C/C++|IOS|C#|Swift|Android|JavaScript|易语言|

服务器之家 - 编程语言 - PHP教程 - phpmailer绑定邮箱的实现方法

phpmailer绑定邮箱的实现方法

2021-04-01 16:36牛逼的霍啸林 PHP教程

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

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

效果如下:

phpmailer绑定邮箱的实现方法

phpmailer绑定邮箱的实现方法

1.配置

?
1
2
3
4
5
6
7
8
9
10
11
12
<?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.发送函数

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
// 发送邮件
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中,并发送

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
// 发送邮箱激活码
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.验证并绑定

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
// 绑定邮箱
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生成树的方法

    这篇文章主要介绍了PHP生成树的方法,实例分析了php树与节点操作的相关技巧,具有一定参考借鉴价值,需要的朋友可以参考下...

    愤怒的土豆3842020-11-09
  • PHP教程PHP中key和current,next的联合运用实例分析

    PHP中key和current,next的联合运用实例分析

    这篇文章主要介绍了PHP中key和current,next的联合运用,结合实例形式分析了key和current,next操作数组元素的相关技巧,需要的朋友可以参考下...

    Fanteathy11822021-01-05
  • PHP教程php商品对比功能代码分享

    php商品对比功能代码分享

    前段时间,朋友叫我帮他写个商品对比功能,当时正好在逛绿森数码商城,点开商品列表,看到有商品对比功能,然后直接下载了他的JS滚动文件和抽出了...

    PHP教程网2012020-11-20
  • PHP教程PHP扩展开发入门教程

    PHP扩展开发入门教程

    这篇文章主要介绍了PHP扩展开发入门教程,本文讲解了使用C语言在Linux系统下开发一个PHP扩展应该具备的最基本知识,需要的朋友可以参考下...

    php中文网1892020-09-06
  • PHP教程PHP正则解析多重循环模板示例

    PHP正则解析多重循环模板示例

    这篇文章主要介绍了PHP正则解析多重循环模板,结合实例形式分析了php基于正则的循环遍历与解析相关操作技巧,需要的朋友可以参考下 ...

    WhoAmMe5412019-09-20
  • PHP教程thinkphp分页实现效果

    thinkphp分页实现效果

    大量数据的显示就需要对内容进行分页,本文章就是就是介绍thinkphp分页进行整理,有需要的朋友一起来了解一下。...

    suger11822021-03-11
  • PHP教程php cli 小技巧

    php cli 小技巧

    很简单,特别方便php做一些cli应用的调试,需要的朋友可以参考下 ...

    PHP教程网3292020-03-31
  • PHP教程PHP中的类型提示(type hinting)功能介绍

    PHP中的类型提示(type hinting)功能介绍

    这篇文章主要介绍了PHP中的类型提示(type hinting)功能介绍,本文讲解了类型提示的作用和使用方法以及使用示例,需要的朋友可以参考下...

    PHP教程网5002020-10-28