PHP實現郵件群發機
原理:
第一:申請大量的郵箱帳號 騰訊的 126的 163的 新浪的 雅虎的 谷歌的 等等提供smtp服務的
第二:收集你要發送郵件的郵件列表
第三:遍歷郵件列表,每一個郵件發送郵件
第四:用linux下定時腳本后臺運行 我跑了兩天 哈哈 ,因為為了盡可能的保證每一個都發送成功
原理是很簡單,但是其中還是遇到很多的問題。
第一:郵件失敗的問題,我這里采用兩個方式解決,第一把大量的那些郵箱帳號依次去發送,直到成功。然后每次發送500封郵件 后把發送郵箱列表隨機打散(避免一個持續失敗)。
我用的是phpmailer
多的不多說了上代碼了。phpmailer 大家自己搜索下載吧
入口腳本:就是linux 執行的 mail_001.php
<?php
define('ROOT_PATH',dirname(__FILE__));
require_once 'library/senderApi.php'; //發送郵件接口
require_once 'config/senderListConf.php'; //發送郵件帳號列表
//讀取文本 郵件地址
shuffle($senderListConf);
$filename = "data/email.txt";
$fp = fopen($filename,"r");
$contents = fread($fp,filesize($filename));
$list_email=explode("\r\n",$contents);
$len=count($list_email);
fclose($fp);
//郵件內容為網頁上的內容
$htmlContent = file_get_contents('http://taobao.1080www.com');
// 參數說明(發送到, 郵件主題, 郵件內容, 附加信息, 用戶名)
foreach($list_email as $key=>$value)
{
$value = trim($value);
$rs=explode("@",$value);
$user_name = $rs['0'];
smtp_mail($value, '秋季補水化妝品_秋季護膚皮膚保養_秋天皮膚干燥怎么辦-小常識',$htmlContent, 'http://taobao.1080www.com/', $user_name,$senderListConf,0);
sleep(3);
if($key%200 == 0 && $key>0)
{
shuffle($senderListConf); //每200次 吧發送郵件順序打亂一次
}
}
die('SendingOver.................'."\r\n");
?>
封裝phpmailer senderApi.php
<?php
// 請求 PHPmailer類 文件
error_reporting(E_ERROR);
require_once("phpmailer/phpmailer.class.php");
// 寫入發送結果函數
function info_write($filename,$info_log)
{
$info.= $info_log;
$info.="\r\n";
$fp = fopen ($filename,'a');
fwrite($fp,$info);
fclose($fp);
}
//發送Email函數
function smtp_mail ( $sendto_email, $subject, $body, $extra_hdrs, $user_name,$senderListConf,$sender=0) {
$batch_no = date("Y_m_d_H");
$mail = new PHPMailer();
$mail->IsSMTP();
$sender_info = $senderListConf[$sender];
if(!$sender_info) 'die 發送帳號出錯了..............'; // send via SMTP
$mail->Host = $sender_info['Host']; // SMTP servers
$mail->SMTPAuth = true; // turn on SMTP authentication
$mail->Username = $sender_info['Username']; // SMTP username 注意:普通郵件認證不需要加 @域名
$mail->Password = $sender_info['Password']; // SMTP password
$mail->From = $sender_info['Username']; // 發件人郵箱
$mail->FromName = "淘寶推薦---TaoBao"; // 發件人 ,比如 中國資金管理網
$mail->CharSet = "UTF-8"; // 這里指定字符集!
$mail->Encoding = "base64";
$mail->AddAddress($sendto_email,$user_name); // 收件人郵箱和姓名
$mail->AddReplyTo("ken@cscsws.com","淘寶推薦");
//$mail->WordWrap = 50; // set word wrap
//$mail->AddAttachment("/var/tmp/file.tar.gz"); // attachment 附件1
//$mail->AddAttachment("/home/www/images/zhuanti/qiujibushui/qiujibushui_attache.jpg", "new.jpg"); //附件2
$mail->IsHTML(true); // send as HTML
$mail->Subject = $subject;
// 郵件內容 可以直接發送html文件
$mail->Body = $body;
$mail->AltBody ="text/html";
if($mail->Send())
{
info_write(ROOT_PATH.'/log/'.$batch_no."ok.txt","$user_name 發送成功");
}
else
{
info_write(ROOT_PATH.'/log/'.$batch_no."falied.txt","$user_name 失敗,發送賬號".$sender_info['Username'].",錯誤信息$mail->ErrorInfo");
if($senderListConf[$sender+1])
{
$sender = smtp_mail ( $sendto_email, $subject, $body, $extra_hdrs, $user_name,$senderListConf,($sender+1));
}
}
return $sender;
}
發送郵件列表示例 senderListConf.php
<?php
$senderListConf = array(
//搜狐郵箱
array('Host'=>'smtp.sohu.com','Username'=>'youemail@sohu.com','Password'=>'yourpassword'),
//雅虎郵箱
array('Host'=>'smtp.mail.yahoo.com','Username'=>'youemail@yahoo.cn','Password'=>'yourpassword'),
//163郵箱
array('Host'=>'smtp.163.com','Username'=>'youemail@163.com','Password'=>'yourpassword'),
//126郵箱
array('Host'=>'smtp.126.com','Username'=>'youemail@126.com','Password'=>'yourpassword'),
//qq郵箱
array('Host'=>'smtp.qq.com','Username'=>'youemail@qq.com','Password'=>'yourpassword'),
//谷歌郵箱
array('Host'=>'smtp.gmail.com','Username'=>'youemail@gmail.com','Password'=>'yourpassword'),
//139郵箱
array('Host'=>'smtp.139.com','Username'=>'youemail@139.com','Password'=>'yourpassword'),
//qq郵箱
array('Host'=>'smtp.qq.com','Username'=>'youemail@qq.com','Password'=>'(yourpassword'),
);
本文由用戶 SimoneNewco 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!