邮箱注册配置:
- 注册163或qq邮箱,开启smtp服务 25端口 ssl则465端口
下载phpmailer
- composer 安装phpmailer
composer require phpmailer/phpmailer
设置配置文件
- 配置文件
书写代码
- 代码
<?php
namespace app\job;
use think\facade\Log;
use think\Queue;
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;class SendEmail
{public function fire($job,$data){print ("k开始发邮件 \n");// 实例化PHPMailer对象// $mail = new PHPMailer(true);try {$isJob = $this->doHelloJob($data);print_r('job->attempts()'.$job->attempts());// 发送邮件if ($isJob) {$job->delete();echo "邮件发送成功!\n";} else {if ($job->attempts() > 3) {//通过这个方法可以检查这个任务已经重试了几次了print_r("<warn>Hello Job has been retried more than 3 times!" . "</warn>\n");$job->delete();}echo ('邮件发送失败');// 如果发送失败,记录错误并释放任务重试}} catch (Exception $e) {// 捕获PHPMailer异常echo ('邮件发送失败');Log::error("邮件发送异常",$e->getMessage());throw new \Exception("邮件发送异常:" . $e->getMessage());}}private function doHelloJob($data) {//实例化PHPMailer核心类$mail = new PHPMailer();$mail->SMTPOptions = array('ssl' => array('verify_peer' => false,'verify_peer_name' => false,'allow_self_signed' => true));// 设置PHPMailer参数//$mail->SMTPDebug = 2; // 调试模式输出$mail->isSMTP(); // 使用SMTP$mail->Host = config('mail.host'); // SMTP服务器$mail->SMTPAuth = true; // 启用SMTP认证$mail->CharSet = 'UTF-8'; #设置发送的邮件的编码$mail->FromName = config('mail.username'); // SMTP用户名$mail->Username = config('mail.from_email'); // SMTP用户名$mail->Password = config('mail.password'); // SMTP密码$mail->From = config('mail.from_email'); #设置发件人邮箱地址 同登录账号$mail->SMTPSecure = 'ssll'; // 安全协议$mail->Port = config('mail.port'); // SMTP端口$mail->isHTML(true);// 设置邮件内容//$mail->setFrom('from-email@example.com', '发件人名称');$mail->addAddress($data['to']); // 收件人$mail->Subject = $data['subject'];$mail->Body = $data['body'];// 发送邮件if ($mail->send()) {return true;} else {Log::error("邮件发送异常",$mail->ErrorInfo());return false;}}
}
结果
异步队列发送邮箱
1 下载消息队列
thinkphp版本不同,则queue版本也不同,一下是tp5.1为例
composer require topthink/think-queue:2.0.4
return [//Redis驱动'connector' => 'Redis',"expire"=>60,//任务过期时间默认为秒,禁用为null"default"=>"default",//默认队列名称"host"=>'127.0.0.1',//Redis主机IP地址"port"=>6379,//Redis端口"password"=>'a123456789',//Redis密码"select"=>0,//Redis数据库索引"timeout"=>0,//Redis连接超时时间"persistent"=>false,//是否长连接];
队列代码编写
<?php
namespace app\job;
use think\facade\Log;
use think\Queue;
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;class SendEmail
{public function fire($job,$data){print ('k开始发邮件');// 实例化PHPMailer对象// $mail = new PHPMailer(true);try {Log::error("k开始发邮件",['msg'=>'eeeeeeeeee']);//实例化PHPMailer核心类$mail = new PHPMailer();$mail->SMTPOptions = array('ssl' => array('verify_peer' => false,'verify_peer_name' => false,'allow_self_signed' => true));// 设置PHPMailer参数//$mail->SMTPDebug = 2; // 调试模式输出$mail->isSMTP(); // 使用SMTP$mail->Host = config('mail.host'); // SMTP服务器$mail->SMTPAuth = true; // 启用SMTP认证$mail->CharSet = 'UTF-8'; #设置发送的邮件的编码$mail->FromName = config('mail.username'); // SMTP用户名$mail->Username = config('mail.from_email'); // SMTP用户名$mail->Password = config('mail.password'); // SMTP密码$mail->From = config('mail.from_email'); #设置发件人邮箱地址 同登录账号$mail->SMTPSecure = 'ssl'; // 安全协议$mail->Port = config('mail.port'); // SMTP端口$mail->isHTML(true);// 设置邮件内容//$mail->setFrom('from-email@example.com', '发件人名称');$mail->addAddress($data['to']); // 收件人$mail->Subject = $data['subject'];$mail->Body = $data['body'];// 发送邮件if ($mail->send()) {// 如果发送成功,删除任务$job->delete();echo "邮件发送成功!\n";} else {echo ('邮件发送失败');// 如果发送失败,记录错误并释放任务重试Log::error("邮件发送异常",$mail->ErrorInfo());throw new \Exception("邮件发送失败:" . $mail->ErrorInfo);}} catch (Exception $e) {// 捕获PHPMailer异常echo ('邮件发送失败');Log::error("邮件发送异常",$e->getMessage());throw new \Exception("邮件发送异常:" . $e->getMessage());}}
}
调用
use think\Queue;$data = ['to' => 'sky_oo8@163.com','subject' => config('mail.subject'),'body' =>$message,];Queue::push('app\job\SendEmail', $data, 'email');
使用命令执行
#生产时候使用 守护进程
php think queue:work --daemon --queue email# 无守护进程
php think queue:work --queue email# 测试开发时候用,会显示细节
php think queue:listen --queue email
注意点
阿里云和腾讯服务器都禁用了25端口,所以需要开启ssl发送邮件