<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                ??碼云GVP開源項目 12k star Uniapp+ElementUI 功能強大 支持多語言、二開方便! 廣告
                [搭建郵件服務器,過程非常簡單](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); } } } ```
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看