php+mysql 模擬隊列發送郵件


1.使用場景

主要解決PHP開發過程中,常見的發送多封郵件時頁面卡死的問題,如果需要給網站所有用戶發送一封系統通知郵件,假設網站有10000個注冊用戶,發送每封郵件需要0.1秒,直接發送導致頁面耗時長卡死,因此就要用到隊列的知識.

2.建表語句

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;

3.文件結構

4.send.php代碼(send.php主要是php+mysql模擬隊列發送郵件,直接運行該文件頁面刷新按鈕會一直轉,直到程序執行結束)

 1 <?php 
 2 //引入類
 3 $rootPath = dirname(__FILE__);
 4 require $rootPath.'/PHPMailer/class.phpmailer.php';
 5 require $rootPath.'/PHPMailer/class.smtp.php';
 6 
 7 function sendMail($toEmail, $toName) {
 8     global $rootPath;
 9     //實例化類
10     $mail = new PHPMailer;
11 
12     $mail->isSMTP();                                          // Set mailer to use SMTP
13     $mail->Host = 'smtp.163.com';                             // Specify main and backup SMTP servers
14     $mail->SMTPAuth = true;                                   // Enable SMTP authentication
15     $mail->Username = '17606392236@163.com';                  // SMTP username
16     $mail->Password = '';                                     // SMTP password
17     $mail->CharSet = 'UTF-8';
18 
19     $mail->setFrom('17606392236@163.com', '163');
20     $mail->addAddress($toEmail, $toName);                     
21     $mail->addReplyTo('17606392236@163.com', '163');
22 
23     $mail->isHTML(true);                                      // Set email format to HTML
24 
25     $mail->Subject = '主題';
26     $mail->msgHTML(file_get_contents($rootPath.'/content.html'));
27     return $mail->send();    
28 }
29 
30 $link = mysql_connect('localhost', 'root', 'root');
31 mysql_select_db('queue');
32 mysql_query('set names utf8');
33 
34 while(true) {
35     $sql = "SELECT * FROM task_list WHERE status = 0 ORDER BY task_id ASC LIMIT 5";
36     $res = mysql_query($sql);
37     $mailList = array();
38     while($row = mysql_fetch_assoc($res)) {
39         $mailList[] = $row;
40     }
41     
42     if(empty($mailList)) {
43         break;
44     } else {
45         foreach($mailList as $k => $v) {
46             if(sendMail($v['user_email'], 'qq')) {
47                 mysql_query("UPDATE task_list SET status = 1 WHERE task_id = ".$v['task_id']);
48             }
49             sleep(3);
50         }
51     }
52 }
53 
54 ?>

5.用戶注冊 ajax異步觸發郵件隊列(郵件隊列會在后台默默執行,不會使前台頁面卡死)

流程圖

reg.php代碼

 1 <!DOCTYPE html>
 2 <html lang="en">
 3 <head>
 4     <meta charset="UTF-8">
 5     <title>注冊</title>
 6     <style>    
 7         form{
 8             text-align: center;
 9             padding-top:200px;
10         }
11     </style>
12     <script text="text/javascript" src="jquery.js"></script>
13 </head>
14 <body>
15     <form action="" method="post">
16         <span>郵箱</span><input type="text" name="user_email" /><br /><br />
17         <span>密碼</span><input type="password" name="user_password" /><br /><br />
18         <input type="submit" name="reg" value="注冊"><br />
19     </form>
20     <?php  
21         if(isset($_POST['reg'])) {
22             $link = mysql_connect('localhost', 'root', 'root');
23             mysql_select_db('queue');
24             mysql_query('set names utf8');
25 
26             $user_email = $_POST['user_email'];
27             $user_password = $_POST['user_password'];
28 
29             $sql = "SELECT * FROM users WHERE user_email = '$user_email'";
30             $res = mysql_query($sql);
31 
32             if(mysql_fetch_row($res)) {
33                 echo "<script>alert('用戶已經存在')</script>";
34             } else {
35                 $sql = "INSERT INTO users(user_email, user_password) VALUES('$user_email', '$user_password')";
36                 $res = mysql_query($sql);
37                 if($res) {
38                     $sql = "INSERT INTO task_list(user_email, status, create_time, update_time) VALUES("."'$user_email',0,"."'".date("Y-m-d H:i:s")."',"."'".date("Y-m-d H:i:s")."'".")";
39                     $res = mysql_query($sql);
40                     if($res) {
41                         ?>
42                         <script>
43                             $.post('send.php');
44                         </script>
45                         <?php
46                     }
47                 } else {
48                     echo "<script>alert('注冊失敗')</script>";
49                 }
50             }
51         }
52         
53 
54         
55     ?>
56 </body>
57 </html>

content.html代碼

 1 <!DOCTYPE html>
 2 <html lang="en">
 3 <head>
 4     <meta charset="UTF-8">
 5     <title>index</title>
 6 </head>
 7 <body>
 8     <h1>demo</h1>
 9     <table style="color: red;border-collapse: collapse;width: 100%;border: 1px solid red;">
10         <tr>
11             <td>ABCD</td>
12             <td>ABCD</td>
13             <td>ABCD</td>
14         </tr>
15         <tr>
16             <td>ABCD</td>
17             <td>ABCD</td>
18             <td>ABCD</td>
19         </tr>
20     </table>
21 </body>
22 </html>

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM