2011-10-13 89 views
3

我是Phpmailer的新手,我正在使用它來發送批量電子郵件給一千多人從一個免費帳戶。當我將電子郵件發送給一個或兩個人時,該代碼工作正常,但當我將它發送給每個人(包括我自己)時,它都會發送到垃圾郵件。還有一個問題是電子郵件的詳細信息,它顯示了所有發送給它的人的電子郵件ID,我不希望這樣做。 的代碼如下:使用phpmailer發送批量郵件

//date_default_timezone_set('America/Toronto'); 

require_once('../class.phpmailer.php'); 
//include("class.smtp.php"); // optional, gets called from within class.phpmailer.php if not already loaded 

$mail = new PHPMailer(); 
$mail->IsSMTP(); // telling the class to use SMTP 
$mail->Host   = "smtp1.site.com;smtp2.site.com"; 
$mail->SMTPAuth  = true;// enable SMTP authentication 
$mail->SMTPKeepAlive = true;// SMTP connection will not close after each email sent 
$mail->Host   = "mail.yourdomain.com"; // sets the SMTP server 
$mail->Port   = 26;     // set the SMTP port for the server 
$mail->Username  = "[email protected]"; // SMTP account username 
$mail->Password  = "yourpassword";  // SMTP account password 
$mail->SetFrom('[email protected]', 'List manager'); 
$mail->AddReplyTo('[email protected]', 'List manager'); 
$mail->Subject  = 'Newsletter'; 
$ids = mysql_query($select, $connection) or die(mysql_error()); 
while ($row = mysql_fetch_row($ids)) { 
$mail->AltBody = "To view the message, please use an HTML compatible email viewer!"; 
$mail->MsgHTML($body); 
$mail->AddAddress($row[0]); 
$mail->Send();//Sends the email 
} 

回答

3

我想你添加新的地址已發送的電子郵件 - 所以第一封電子郵件會去一個人,發送的第二封電子郵件將前往同一個人再加上另一個,第三個將會再加上另外兩個,依此類推。

此外,我不認爲你需要每次都設置AltBody和MsgHTML。

您應該首先將所有地址添加到BCC字段,然後發送。

因此,嘗試...

// rest of code first 
$mail->AltBody = "To view the message, please use an HTML compatible email viewer!"; 
$mail->MsgHTML($body); 
$mail->AddAddress("[email protected]") 

$ids = mysql_query($select, $connection) or die(mysql_error()); 
while ($row = mysql_fetch_row($ids)) { 
    $mail->AddBCC($row[0]); 
} 

$mail->Send();//Sends the email 
+0

Marc B的代碼比我的原始代碼更好 - 我描述的是正確的,但是我的代碼並不反映BCC的建議。你應該按照他的例子來添加一個「to」接收者(通常是你自己,使用AddAddress'''),然後使用AddBCC'作爲列表成員的電子郵件。我編輯了我的代碼來反映這一點。謝謝Marc! – JoLoCo

+0

不僅你不需要每次都設置de MsgHTML,如果插入的文本不同於每個郵件 – Circum

1

使用BCC(密件抄送)隱藏收件人列表。 與垃圾郵件問題有關,它取決於收件人的電子郵件提供商,垃圾郵件是什麼,什麼不是垃圾郵件,並且存在很多因素。

12

正如JoLoCo指出的那樣,AddAddress()方法只是將新地址添加到現有收件人列表中。而且,由於您是以添加/發送循環的方式進行操作,因此您會向第一個收件人發送大量重複副本,而第二個收件人則少了一個,等等......

您需要的是:

while($row = mysql_fetch_row(...)) { 
    $mail->AddAddress($row[0]); 
    $mail->send(); 
    $mail->ClearAllRecipients(); // reset the `To:` list to empty 
} 

另一方面,由於這樣會使郵件服務器發送大量單個電子郵件,另一個選項是生成一個SINGLE電子郵件,併爲所有收件人分配BCC。

$mail->AddAddress('[email protected]'); // send the mail to yourself 
while($row = mysql_fetch_row(...)) { 
    $mail->AddBCC($row[0]); 
} 
$mail->send(); 

該選項很可能是優選的。您只生成一封電子郵件,並讓郵件服務器處理向每個收件人發送副本的繁重工作。

+0

這可能會產生不好的效果.123密碼的問題在於它是垃圾郵件。我在兩個或三個不同的賬戶上測試了它們,它們都是垃圾郵件。 – user992654

+0

您必須做其他事情,比如實施域密鑰和SPF記錄,並且可能會將您的郵件轉移到更合理的發送平臺,以減輕垃圾郵件問題。但除此之外,只需切換至使用「To」即可:相反,知道PHP必須爲每個收件人生成一封電子郵件。 –

+0

感謝您的好腳本,我知道它的電子郵件地址爲空$ mail-> ClearAllRecipients() –