企业🤖AI Agent构建引擎,智能编排和调试,一键部署,支持私有化部署方案 广告
[搭建邮件服务器,过程非常简单](https://blog.csdn.net/gyxuehu/article/details/78500645) 使用队列方式异步发送邮件防页面卡死,学完就知道强大之处 一、邮件发送原理 ![](https://img.kancloud.cn/e4/87/e4873044d411b5731e0bd7e03f1fc62d_918x500.png) 二、利用[phpmailer](https://github.com/PHPMailer/PHPMailer)类实现邮件发送 ~~~ <?php // Import PHPMailer classes into the global namespace // These must be at the top of your script, not inside a function use PHPMailer\PHPMailer\PHPMailer; use PHPMailer\PHPMailer\SMTP; use PHPMailer\PHPMailer\Exception; // Load Composer's autoloader require 'vendor/autoload.php'; // Instantiation and passing `true` enables exceptions $mail = new PHPMailer(true); try { //Server settings $mail->SMTPDebug = SMTP::DEBUG_SERVER; // Enable verbose debug output $mail->isSMTP(); // Send using SMTP $mail->Host = 'smtp1.example.com'; // Set the SMTP server to send through $mail->SMTPAuth = true; // Enable SMTP authentication $mail->Username = 'user@example.com'; // SMTP username $mail->Password = 'secret'; // SMTP password $mail->charset ='UTF-8'; $mail->SMTPSecure = PHPMailer::ENCRYPTION_STARTTLS; // Enable TLS encryption; `PHPMailer::ENCRYPTION_SMTPS` also accepted $mail->Port = 587; // TCP port to connect to //Recipients $mail->setFrom('from@example.com', 'Mailer'); $mail->addAddress('joe@example.net', 'Joe User'); // Add a recipient $mail->addAddress('ellen@example.com'); // Name is optional $mail->addReplyTo('info@example.com', 'Information'); $mail->addCC('cc@example.com'); $mail->addBCC('bcc@example.com'); // Attachments $mail->addAttachment('/var/tmp/file.tar.gz'); // Add attachments $mail->addAttachment('/tmp/image.jpg', 'new.jpg'); // Optional name // Content $mail->isHTML(true); // Set email format to HTML $mail->Subject = '主题'; $mail->Body = 'This is the HTML message body <b>in bold!</b>'; $mail->AltBody = 'This is the body in plain text for non-HTML mail clients'; $mail->msgHTML(file_get_contents('./test.html')); $mail->send(); echo 'Message has been sent'; } catch (Exception $e) { echo "Message could not be sent. Mailer Error: {$mail->ErrorInfo}"; } ~~~ 三、为什么需要队列 我们需要给网站所有用户发送一封系统通知邮件,假设网站有10000个注册用户,发送每封邮件需要0.1秒,执行一次发送通知操作需要多长时间?10000 x 0.1 = 1000 四、在phpcli模式下测试队列 建表: ``` create table users ( user_id int(5) not null auto_increment, user_email varchar(40) not null, user_password char(32) not null, primary key(user_id) )engine=myisam default charset=utf8; create table task_list ( task_id int(5) not null auto_increment, user_email varchar(40) not null, status int(2) not null, create_time datetime not null, update_time datetime not null, primary key(task_id) )engine=myisam default charset utf8; insert into task_list(user_email,status,create_time,update_time) VALUES( 'phpjiaoxuedev@sina.com', 0, now(), now() ); insert into task_list(user_email,status,create_time,update_time) VALUES( 'phpjiaoxuedev1@sina.com', 0, now(), now() ); ``` 五、Ajax异步触发队列 1. 建立用户表存储注册的用户 2. 实现注册功能,当注册成功之后,把用户email插入邮件队列表中 3. 使用ajax触发队列 ![](https://img.kancloud.cn/81/b8/81b8eb35f8c369496fed5d1106f7cde2_944x472.png) 前台代码 ![](https://img.kancloud.cn/d5/c7/d5c7f71e33897bdf04f4415eb73316f2_583x103.png) ![](https://img.kancloud.cn/a7/09/a7094cff806668b4267d156fbafe99cf_692x461.png) do_queue.php ``` <?php exec("/php7/php.exe queue.php"); ``` queue.php ``` <?php use PHPMailer\PHPMailer\PHPMailer; use PHPMailer\PHPMailer\SMTP; use PHPMailer\PHPMailer\Exception; // Load Composer's autoloader require 'vendor/autoload.php'; function sendEmail($host, $fromEmail, $fromPwd, $fromName, $toEmail, $toName, $subject, $content){ $mail = new PHPMailer(true); try { //Server settings $mail->SMTPDebug = SMTP::DEBUG_SERVER; // Enable verbose debug output $mail->isSMTP(); // 设置邮件使用 SMTP $mail->Host = 'smtp1.example.com'; // 邮箱服务器地址 $mail->SMTPAuth = true; // 启用 SMTP 身份验证 $mail->charset = 'UTF-8'; $mail->Username = 'user@example.com'; // SMTP 邮箱地址 $mail->Password = 'secret'; // 邮箱密码 $mail->Encoding = 'base64'; // 使用base64加密邮箱和密码 //Recipients $mail->setFrom('from@example.com', 'Mailer'); //发件人邮件地址 $mail->addAddress('joe@example.net', 'Joe User'); // 增加一个收件人 $mail->addAddress('ellen@example.com'); // 再增加一个收件人,名称是可选的 // Content $mail->isHTML(true); // Set email format to HTML $mail->Subject = 'Here is the subject'; $mail->Body = 'This is the HTML message body <b>in bold!</b>'; $mail->send(); } catch (Exception $e) { echo "Message could not be sent. Mailer Error: {$mail->ErrorInfo}"; } } $link=mysql_connect('localhost','root','root'); mysql_select_db($link,$dbname); mysql_query("set names utf8"); while (true) { $sql="select * from tast_list where status = 0 order by task_id asc limit 5"; $res=mysql_query($sql); $mailList=[]; while ($row=mysql_fetch_assoc($res)) { $mailList[]=$row; } if (empty($mailList)) { break; }else{ foreach ($mailList as $key => $value) { if (sendMail ("smtp.aliyun.com","phpjiaoxuedev@aliyun.com", "aliemail123","aliyun", $v['email'], 'sina', "php+mysql模拟队列发送邮件课程",file_get_contents( "new_course.html" ) )){ mysql_query( "UPDATE task_list SET status = 1 WHERE task_id =". Svalue['taskid'] ); } sleep(3); } } } ```