[英]Sending bulk e-mail using phpmailer
我正在嘗試使用phpmailer發送批量電子郵件,當我單擊“發送”按鈕時,我的數據庫中有10個以上的電子郵件ID,然后第一個電子郵件將發給一個人,第二個電子郵件將發給該人再加上另一個,第三個將去那兩個加一,依此類推。 這是我的編碼,請幫助我
<?php
$body=$_POST['message'];
$subject=$_POST['sub'];
//error_reporting(E_ALL);
error_reporting(E_STRICT);
date_default_timezone_set('America/Toronto');
require_once("class.phpmailer.php");
//include("class.smtp.php");
$mail = new PHPMailer();
$mail->IsSMTP(); // telling the class to use SMTP
$mail->Host = "stmp.gmail.com"; // SMTP server
$mail->SMTPDebug = 1; // enables SMTP debug information
// 1 = errors and messages
// 2 = messages only
$mail->SMTPAuth = true; // enable SMTP authentication
$mail->SMTPSecure = 'ssl';
$mail->Host = "smtp.gmail.com"; // sets GMAIL as the SMTP server
$mail->Port = 465; // set the SMTP port for the GMAIL server
$mail->CharSet = "big5";
$mail->Username = "abc@gmail.com"; // GMAIL username
$mail->Password = "**********"; // GMAIL password
$mail->SetFrom("abc@gmail.com", ''); // set reply id
$mail->Subject = ($subject); // subject
$mail->MsgHTML("$body"); // message
$mail->AddAddress($address, "abc");
$con=mysql_connect("localhost","root","") or
die("could not connect:".mysql_error());
mysql_select_db("bulkemail");
$qry=mysql_query("SELECT * FROM email_id", $con);
if(!$qry)
{
die("Query Failed: ". mysql_error());
}
while($row = mysql_fetch_array($qry))
{
$id= $row["email"];
$address = ($id);
$mail->AddBcc($id);
$mail->send();
if(!$mail->Send()) {
echo "Mailer Error: " . $mail->ErrorInfo;
}
else {
echo "Message sent!";
}
}
?>
刪除$mail->send();
並將if(!$mail->Send())
移到while($row ..)
循環之外
while($row = mysql_fetch_array($qry)){
$id= $row["email"];
$address = ($id);
$mail->AddBcc($id);
} // end the while loop
// remove $mail->send(); as it is a duplicate of if(!$mail->Send())
if(!$mail->Send()) {
echo "Mailer Error: " . $mail->ErrorInfo;
}
else {
echo "Message sent!";
}
我曾經用phpmailer進行群發郵件,但我遇到了同樣的問題。
while($row = mysql_fetch_array($qry)){
$id= $row["email"];
$address = ($id);
$mail->AddBcc($id);
//imo if should be in the while loop.
if(!$mail->Send()) {
echo "Mailer Error: " . $mail->ErrorInfo;
}
else {
echo "Message sent!";
//I added this function down there so that old address would be removed but in the new loop, new one will be added.
$mail-> ClearAddresses();
}
} // end the while loop
這對我有用。
在您的情況下,您要將新的密件抄送添加到循環的每個重復中已發送的所有密件抄送的列表中。
就像“ kworr 13 Sep 27 '13:36”所說,您需要為循環中的每個發送創建新實例。
或者你需要把
$mail->send();
跳出循環。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.