laravel发送邮件验证码

一、下载
composer require phpmailer/phpmailer
https://github.com/PHPMailer/PHPMailer
二、开启网易和QQ邮箱的SMTP服务
1.163
–1.进入到 设置 POP3/SMTP/IMAP设置页面
–2.手动开启,并设置客户端授权密码 zwd1111(我的)
2.qq
–1.设置->账户 最下面开启第一个服务
–2.设置客户端授权密码 mqfxwn
3.php_openssl 和php_socket 这个必须要开启的
三、编写代码

第一种方式

namespace App\Http\Controllers\Home;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;

public  function  sendMail()  
        {  

            $mail = new PHPMailer(true);             
            $mail->isSMTP(); // tell to use smtp        
            $mail->CharSet = "utf-8"; // set charset to utf8  
            $mail->SMTPAuth = true;  // use smpt auth  
            $mail->SMTPSecure = "tls"; // or ssl  
            $mail->Host = "smtp.163.com";  

            $mail->Port = 25; 
            $mail->Username = "[email protected]";  
            $mail->Password = "*****";//去开通的qq或163邮箱中找,这里用的不是邮箱的密码,而是开通之后的一个token 

            $mail->setFrom("[email protected]", "www");//设置邮件来源  //发件人
            //标题内容都是网站管理员设定好的
            $mail->Subject = "Test"; //邮件标题 
            $mail->MsgHTML("Thisisatest");   //邮件内容

            $mail->addAddress("[email protected]", "leon");  //收件人(用户输入的邮箱)
            $res = $mail->send();  
            if($res){   
                dd($res);
            }else{
                return 12121;
            }
        }  

猜你喜欢

转载自blog.csdn.net/weixin_42597707/article/details/82384912