###**1. 建表存储需要发送的邮件**
```sql
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 `muke`.`task_list` (`task_id`, `user_email`, `status`, `create_time`, `update_time`) VALUES ('1', '*******@qq.com', '0', '2017-03-06 10:53:41', '2017-03-06 10:53:41');
```
###**2. 使用php+mysql轮询实现队列**
```
$rootPath = dirname(__FILE__);
require $rootPath.'/PHPMailer/PHPMailerAutoload.php';
function sendMail($host, $fromEmail, $fromPwd, $fromName, $toEmail, $toName, $subject, $content) {
$mail = new PHPMailer;
$mail->isSMTP(); // 设置邮件使用SMTP
$mail->Host = $host; // 邮件服务器地址
$mail->SMTPAuth = true; // 启用SMTP身份验证
$mail->CharSet = 'UTF-8'; // 设置邮件编码
$mail->Encoding = 'base64'; // 使用base64加密邮箱和密码
$mail->Username = $fromEmail; // SMTP 用户名,即个人的邮箱地址
$mail->Password = $fromPwd; // SMTP 密码,即个人的邮箱密码
$mail->From = $fromEmail; // 发件人邮箱地址
$mail->FromName = $fromName; // 发件人名称
$mail->addAddress($toEmail, $toName); // 添加接受者
$mail->isHTML(true); // 设置邮件格式为HTML
$mail->Subject = $subject;
$mail->msgHTML($content);
return $mail->send();
}
$link = mysql_connect('192.168.2.110', 'root', 'root');
mysql_select_db('muke');
mysql_query("set names utf8");
while (true) {
$sql = 'select * from task_list where status=0 order by task_id asc limit 5';
$res = mysql_query($sql);
$mailList = array();
while ($row = mysql_fetch_assoc($res)) {
$mailList[] = $row;
}
if(empty($mailList)) {
break;
} else {
foreach ($mailList as $key => $value) {
if (sendMail("smtp.aliyun.com", '*****@aliyun.com', '******', 'aliyun', $value['user_email'], 'vision', '模拟队列发送邮件', file_get_contents($rootPath.'/sendmail.html'))) {
mysql_query('update task_list set status=1 where task_id='.$value['task_id']);
echo $value['user_email']."\n";
}
sleep(3);
}
}
}
echo "done\n";
```
###**3. 在cli模式下测试队列**
```
/user/bin/php /var/www/sendmail/queue.php
```