thinkphp phpmailer邮箱验证

thinkphp 关于phpmailer的邮箱验证

一  、

  登陆自己的邮箱,例如:qq邮箱。登陆qq邮箱在账户设置中开启smtp服务:

  

之后回发送一个授权码 , 这个授权码先保存下来,这个授权码在后面会用得到。

二、

  使用composer 下载 phpmailer 

  在cmd中打开你的tp框架路径然后直接 输入 composer  require phpmailer/phpmailer

  之后你的第三方类库下面会多一个phpmailer文件夹,打开此文件夹;

  

  然后将src的资源复制下来;

  在tp框架的extends文件夹下面创建一个phpmailer文件夹;

  把刚才复制的资源粘贴在此文件夹下面。

  

修改三个文件的命名空间: namespace phpmailer.

三、使用phpmailer

  在tp框架下面的common.php里面写入:

  

<?php
    
    function sendMail($mail , $to ,$title , $content)
    {
      try{
 $mail->SMTPDebug = 0;  //SMTP调试功能 0=关闭, 1=错误和消息 2=消息
 $mail->isSMTP(); 设定使用SMTP服务;
 $mail->CharSet = 'utf-8'; //邮件编码;
 $mail->Host = 'smtp.qq.com'; //smtp服务器;
 $mail->SMTPAuth = true; //启用smtp验证功能;
 $mail->Username = '******@qq.com'; //SMTP服务器用户名;
 $mail->Password = '**********'; //这个是你开始获取到的授权码;也可以是你的邮箱密码;
$mail->SMTPSecure = 'ssl'; //使用安全协议;

    //recipitents  //收件人信息设置
$mail->setForm('*******@qq.com' , '为了php'); //第一个参数是收件人邮箱 , 第二个参数是邮件主题;
$mail->addAddress($to); //传入发件人的邮箱地址; 

//Content邮件内容
$mail->isHTML(true); 
$mail->Subject = $title;
$mail->Body = $content;
return $mail->send()
}  catch (Exception $e){
 echo 'Message could not sent.Mailer Error:',$email->ErrorInfo;    
}

}                                        

在 application conttoller 的index.php文件中 use phpmailer/PHPMaileron

<?php

use appindexcontroller;
use thinkController;
use thinkView;
use phpmailerPHPMailer;

class Index extends Controller
{
  public $view;
  public function __construct()
{
  $this->view = new View;
}
public function index()
{
  $this->view->fetch('index/index');
}


public function sendemail() { $code = rand(10000 , 99999); $data = array_values($_POST); $user = implode('' , $data); $emailuser = str_replace('' , '.' , $user); $email = new PHPMailer(true); $res = sendMail($mail , $emailuser , 'php真好玩' , '您好!感谢您成为[php真好玩成员] , <br />祝您玩的开心 , 玩的愉快!'); if($res){ return json(['status'=>1 , 'msg'=>'邮箱发送成功']); } else { return json(['status'=>0 , 'msg'=>'邮箱发送失败']); } } }

controller 的view 文件下创建index文件夹 , 在index文件下写一个index.html文件;

<html>
    <head>
        <meta charset="utf-8" />
        <title>index</title>
        
     </head>
    <body>
        <input type="text" placeholder="请输入邮箱" id="email">
        <input type="button" id="btn" value="邮箱验证">
    </body>
    <script src="[这里引用你的jquery路径]"></script>
    <script type="text/javascript">
        $(function(){

            $("#btn").click(function{
    
            $.post(":url('index/index/sendemail')",
            {"email":$("#email").val()},
            function(data){
                if(data.status){
                   alert(data.msg); 
} else {
                    alert(data.msg);
}
})
});

})

    </script>
</html>
原文地址:https://www.cnblogs.com/whrTC/p/9942434.html