Home  >  Article  >  PHP Framework  >  Detailed explanation of how the ThinkPHP framework implements email activation function

Detailed explanation of how the ThinkPHP framework implements email activation function

藏色散人
藏色散人forward
2021-06-11 12:00:043969browse

The following tutorial column of thinkphp framework will introduce to you how the ThinkPHP framework implements the email activation function. I hope it will be helpful to friends in need!

The details are as follows:

The configuration framework adopts the ThinkPHP3.1 framework, as shown below:

Configuration process diagram:

Detailed explanation of how the ThinkPHP framework implements email activation function

1. Modify the configuration as follows:

<?php
return array(
  //&#39;配置项&#39;=>&#39;配置值&#39;
    &#39;MAIL_ADDRESS&#39;=>&#39;shcg666@sohu.com&#39;, // 邮箱地址
    &#39;MAIL_SMTP&#39;=>&#39;smtp.sohu.com&#39;, // 邮箱SMTP服务器
    &#39;MAIL_LOGINNAME&#39;=>&#39;shcg666@sohu.com&#39;, // 邮箱登录帐号
    &#39;MAIL_PASSWORD&#39;=>&#39;******&#39;, // 邮箱密码
);

2. Add a class to the function

<?php
class EmailAction extends Action{
  /*
  * microtime() 函数返回当前 Unix 时间戳和微秒数。
  * mt_srand() 播种 Mersenne Twister 随机数生成器。从 PHP 4.2.0 版开始,seed 参数变为可选项,当该项为空时,会被设为随时数。
  * 注释:自 PHP 4.2.0 起,不再需要用 srand() 或 mt_srand() 函数给随机数发生器播种,现已自动完成。
  * pow — 指数表达式
  */
  //random()这个函数是我用来生成一个随机数的,$numeric = 0生成一个6位的大小写字母与数字混合的字符串。$numeric = 1生成一个6位数字的字符串
  public function random($length = 6 , $numeric = 0) {
  PHP_VERSION < &#39;4.2.0&#39; && mt_srand((double)microtime() * 1000000);
  if($numeric) {
    $hash = sprintf(&#39;%0&#39;.$length.&#39;d&#39;, mt_rand(0, pow(10, $length) - 1));
  } else {
    $hash = &#39;&#39;;
    $chars = &#39;ABCDEFGHJKLMNPQRSTUVWXYZ23456789abcdefghjkmnpqrstuvwxyz&#39;;
    $max = strlen($chars) - 1;
    for($i = 0; $i < $length; $i++) {
      $hash .= $chars[mt_rand(0, $max)];
    }
  }
  return $hash;
  }
  //发送邮件的方法
  public function index(){
    //生成6位激活码
    $random = $this->random(6,0);
    //获取本网站的域名,域名在config里面配置下.
    //例如&#39;domain&#39; => &#39;http://write.blog.csdn.net&#39;,
    $domain = C(&#39;domain&#39;);
    //生成激活码模块地址
    $url = $domain.U("Home/Email/activate")."/?yam=$random";
    //将邮件地址和随机数放入session
    session("shcg666@sohu.com","$random");
    //发送邮件
    SendMail("shcg666@sohu.com","这是邮件标题","将此网址复制到浏览框$url");
  }
}
function SendMail($address,$title,$message){
  //引入文件
  vendor(&#39;PHPMailer.class#PHPMailer&#39;);
  require("phpmailer/class.phpmailer.php");
  require("phpmailer/class.smtp.php");
  //实例化邮件类
  $mail=new PHPMailer();
  // 设置PHPMailer使用SMTP服务器发送Email
  $mail->IsSMTP();
  // 设置邮件的字符编码,若不指定,则为&#39;UTF-8&#39;
  $mail->CharSet=&#39;UTF-8&#39;;
  // 添加收件人地址,可以多次使用来添加多个收件人
  $mail->AddAddress($address);
  // 设置邮件正文
  $mail->Body=$message;
  // 设置邮件头的From字段。//发件人
  $mail->From=C(&#39;MAIL_ADDRESS&#39;);
  // 设置发件人名字
  $mail->FromName=&#39;LilyRecruit&#39;;
  // 设置邮件标题
  $mail->Subject=$title;
  // 设置SMTP服务器。
  $mail->Host=C(&#39;MAIL_SMTP&#39;);
  // 设置为"需要验证"
  $mail->SMTPAuth=true;
  // 设置用户名和密码。
  $mail->Username=C(&#39;MAIL_LOGINNAME&#39;);
  $mail->Password=C(&#39;MAIL_PASSWORD&#39;);
  // 发送邮件。
  return($mail->Send());
}

3. Imported files

Download the PHPMailer package from the Internet and copy class.smtp.php and class.phpmailer.php directly without any changes.

Click the link to download the class.smtp.php file (https://share.weiyun.com/6ECQn7Mq).

For the complete example code of class.phpmailer.php file, click the link to download (https://share.weiyun.com/beakkcPt) .

Configuration is complete, the specific processing method needs further changes.

4. Common mail server (receiving server and sending mail server) addresses

Tencent QQ mailbox
Receiving server: pop.qq.com
Sending Server: smtp.qq.com

Netease 126 mailbox
Receiving server: pop3.126.com
Sending server: smtp.126.com

Netease 163 free mail
Receiving server: pop.163.com
Sending server: smtp.163.com

NetEase 163VIP mailbox
Receiving server: pop.vip.163.com
Sending server: smtp. vip.163.com

NetEase 188 Fortune Mail
Receive server: pop.188.com
Send server: smtp.188.com

NetEase yeah.net mailbox
Receiving server: pop.yeah.net
Sending server: smtp.yeah.net

Netease netease.com mailbox
Receiving server: pop.netease.com
Sending server: smtp. netease.com

Sina paid mailbox
Receiving server: pop3.vip.sina.com
Sending server: smtp.vip.sina.com

Sina free mailbox
Receiving server: pop3.sina.com.cn
Sending server: smtp.sina.com.cn

Sohu Mailbox
Receiving server: pop3.sohu.com
Sending server: smtp. sohu.com

21cnhappymail
Receive server: vip.21cn.com
Sending server: vip.21cn.com

21cn Economic Mail
Receive server: pop .163.com
Sending server: smtp.163.com

tom mailbox
Receiving server: pop.tom.com
Sending server: smtp.tom.com

263mailbox
Receive server: 263.net
Send server: smtp.263.net

NetEase 163.com mailbox
Receive server: rwypop.china.com
Send server :rwypop.china.com

Yahoo mailbox
Receiving server: pop.mail.yahoo.com
Sending server: smtp.mail.yahoo.com

Gmail mailbox
Receive server: pop.gmail.com
Sending server: smtp.gmail.com

Related recommendations:The latest 10 thinkphp video tutorials

The above is the detailed content of Detailed explanation of how the ThinkPHP framework implements email activation function. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:jb51.net. If there is any infringement, please contact admin@php.cn delete