背景:用户经过测评系统之后,会生成一份测评报告,但是由于邮件系统和测评系统是两份独立的系统,某天突然有500封测评报告产生,需要在24小时之内将测评报告发送给测评着。每个人的测评报告都是唯一的,因此附件内容都不一致。
利用PHPMailer批量发送附件内容,只要将用户的附件和邮箱匹配成功就可以发送。因此,这个难题就变成了如何在短时间内发送500封邮件。
再分析一下,就是我的数据库中有500条记录,需要一一处理掉,这不就是又成了熟悉的生产者和消费者的事情了吗?
直接用crontab的定时脚本执行邮件发送的方法就OK了。
代码设置如下:
定时脚本,每隔10秒钟从池子中去消费,逐一发邮件。定时shell脚本如下:
1 #!/bin/sh2 cd /root/pengjun/vipEmail/PHPMailer/3 step=10 #间隔的秒数,不能大于604 for (( i = 0; i < 60; i=(i+step) )); do5 $(/usr/local/php/bin/php 'sendEmail.php')6 sleep $step7 done8 exit 0
*/1 * * * * root sh /root/pengjun/sh/perVipMail.sh
sendEmail.php的代码如下:
"附件") */require('class.phpmailer.php');include_once "./DB/DBHelper.php";define('HOST', '192.168.1.188');define('USER', 'root');define('PASSWORD', 'root');define('DBNAME', 'test');class db extends DBHelper{ function __construct() { parent::__construct(); } public function getEmail(){ $sql = "SELECT name , email from sendEmail where status = 0 limit 1"; $ret = $this->find($sql); return $ret; } public function updateEmail($email){ $sql = "UPDATE sendEmail set status = 1 where email = '{$email}' and status = 0"; return $this->executesql($sql); }}$dbMail = new db();$mail = new PHPMailer(); //实例化$mail->IsSMTP(); // 启用SMTP$mail->Host = "smtp.163.com"; //SMTP服务器 163邮箱例子//$mail->Host = "smtp.126.com"; //SMTP服务器 126邮箱例子//$mail->Host = "smtp.qq.com"; //SMTP服务器 qq邮箱例子$mail->Port = 25; //邮件发送端口$mail->SMTPAuth = true; //启用SMTP认证$mail->CharSet = "UTF-8"; //字符集$mail->Encoding = "base64"; //编码方式$mail->Username = "18025439531@163.com"; //你的邮箱$mail->Password = "#######"; //你的密码$mail->Subject = "【主题】这是邮件的主题"; //邮件标题$mail->From = "18025439531@163.com"; //发件人地址(也就是你的邮箱)$mail->FromName = "彭军"; //发件人姓名$name_email = $dbMail->getEmail();/*$name_email = array( array("name" => "彭军" , "email" =>"pengjun@shixiba.com"), array("name" => "彭小军" , "email" =>"1098325951@qq.com"), array("name" => "彭f军" , "email" =>"pengjun132@foxmail.com"), array("name" => "张琳" , "email" =>"zhanglin@shixiba.com"), array("name" => "邢杰" , "email" =>"xingjie@shixiba.com"), );*/$path = "./sendEmail/";$template = file_get_contents("./template.html");//定义发送模版foreach ($name_email as $key => $value) { $address = $value['email'];//收件人email $mail->AddAddress($address,$value['email']); //添加收件人1(地址,昵称) $attachment = $value['name']."-".$value['email']; $file = $path.$value['email']."-vip.html"; $mail->AddAttachment($file,$attachment.".html"); // 添加附件,并指定名称,,记住要添加附件尾椎 $mail->IsHTML(true); //支持html格式内容 //$mail->AddEmbeddedImage("test.jpg", "my-attach", "test.jpg"); //设置邮件中的图片 $mail->Body = $template; //邮件主体内容 //发送 if(!$mail->Send()) { echo $attachment."===发送失败\n"; file_put_contents("error.txt", $attachment."===发送失败\n" , FILE_APPEND); echo "error: " . $mail->ErrorInfo; continue ; } else { $status = $dbMail->updateEmail($value['email']); if ($status) { echo $attachment."===发送成功\n"; file_put_contents("success.txt", $attachment."===发送成功-".date('Y-m-d H:i:s')."\r\n" , FILE_APPEND); }else{ echo $attachment."===发送失败\n"; file_put_contents("error.txt", $attachment."===发送失败".date('Y-m-d H:i:s')."\r\n", FILE_APPEND); } }}?>
邮件发送,会使用到phpmailer这个类,这个类只需要到网上下载即可。http://download.csdn.net/download/qq_23885541/10024847