使用 邮箱配置 激活码 用于 用户注册激活

时间:2024-03-04 09:33:03

我们使用 phpmailer  插件 完成  

1 引入 文件到 thinkphp 下的  vendor

 

2 在 配置参数 中配置 imap

 

 

 

 1 function sendMail($reciver, $recivername, $subject = "", $body="", $sender="751971678@qq.com", $sendername="刘涛")
 2 {
 3     vendor(\'PHPMailer.PHPMailerAutoload\');
 4 
 5     $phpMailer = new \PHPMailer();
 6 
 7     //发送邮件服务器配置
 8     $phpMailer->isSMTP();                                    // Set mailer to use SMTP
 9     $phpMailer->Host = C(\'MAIL_CONFIG.SMTP_HOST\');  // Specify main and backup SMTP servers
10     $phpMailer->SMTPAuth = true;                               // Enable SMTP authentication
11     $phpMailer->Username = C(\'MAIL_CONFIG.USER_NAME\');                 // SMTP username
12     $phpMailer->Password = C(\'MAIL_CONFIG.PASSWORD\');                           // SMTP password
13     $phpMailer->SMTPSecure = \'ssl\';                            // Enable TLS encryption, `ssl` also accepted
14     $phpMailer->Port = C(\'MAIL_CONFIG.PORTS\');                                    // TCP port to connect to
15 
16     //发送邮件用户配置
17     $phpMailer->setFrom($sender, $sendername);
18     $phpMailer->addAddress($reciver, $recivername);     // Add a recipient
19 
20     $phpMailer->isHTML(true);                                  // Set email format to HTML
21 
22     //邮件内容及主题
23     $phpMailer->Subject = $subject;
24     $phpMailer->Body    = $body;
25 
26     if(!$phpMailer->send()) {
27         throw new \Think\Exception($phpMailer->ErrorInfo, 0);
28     } else {
29         return true;
30     }
31 }

2 php 的 调用  

 1 public function  addUser(){
 2         //验证码正确性验证
 3             $code = I(\'post.captcha\');
 4             if (empty($code)) {
 5                 $this->error(\'请输入验证码\');
 6             }
 7             //实例化verify()
 8             $verifyObj = new Verify();
 9             if (!$verifyObj->check($code)) {
10                 $this->error(\'验证码错误\');
11             }
12             //>>1 实例化用户数据模型
13             $userModel = D(\'User\');
14         //获得邮件信息
15         $activeToken = md5(String::randString(32)).md5(String::randString(32)); //生成邮箱激活码
16         $email = I(\'post.email\');
17 
18         $_POST[\'active_token\'] = $activeToken;
19 
20             //>>3 调用模型的create()来接收数据
21         if ($userModel->create() === false) {
22             $this->error(\'数据有误:\'.dealError($userModel));
23             return;
24         }
25         $activeUrl = "http://".$_SERVER[\'HTTP_HOST\'].U(\'activeUser\', array(\'email\'=> base64_encode($email), \'activetoken\'=>base64_encode($activeToken)));
26 
27         $emailBody = \'<h2><strong>新用户激活:</strong></h2>欢迎注册XXXX网站,请在2小时内点击下边的链接进行激活。如果不是你本人操作请忽略本邮件。<p><a href="\'.$activeUrl.\'">点击激活</a></p>\';
28 
29         //成功将数据加入数据库
30         try{
31             $userModel->addUser();
32             $this->success("注册成功,欢迎来到德莱联盟",U(\'Index/index\'));
33 //            执行发送邮件信息
34             sendMail($email, \'新用户\', \'账号激活\', $emailBody);//用户激活操作
35         }catch (Exception $e){
36             $this->error(\'抱歉注册失败\'.$e->getMessage());
37         }
38     }