使用phpmailer发送批量邮件

use*_*654 4 php email phpmailer

我是Phpmailer的新手,我正在使用它从一个noreply帐户向超过一千人发送批量电子邮件.当我向一两个人发送电子邮件时,代码工作正常,但当我发送给每个人(包括我自己)时,它会转到垃圾邮件.另一个问题是电子邮件的详细信息,它显示了发送给它的所有人的电子邮件ID,我不希望它这样做.代码如下:

//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      = "yourname@yourdomain"; // SMTP account username
$mail->Password      = "yourpassword";        // SMTP account password
$mail->SetFrom('noreply@mydomain.com', 'List manager');
$mail->AddReplyTo('list@mydomain.com', '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
}
Run Code Online (Sandbox Code Playgroud)

Mar*_*c B 12

正如JoLoCo指出的那样,该AddAddress()方法只是将新地址添加到现有收件人列表中.而且,由于你是以添加/发送循环方式进行的,所以你向第一个收件人发送了大量重复的副本,少了一个副本,等等......

你需要的是:

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

另一方面,由于这会使您的邮件服务器发送大量单个电子邮件,另一种选择是生成一封SINGLE电子邮件,并BCC所有收件人.

$mail->AddAddress('you@example.com'); // send the mail to yourself
while($row = mysql_fetch_row(...)) {
   $mail->AddBCC($row[0]);
}
$mail->send();
Run Code Online (Sandbox Code Playgroud)

最有可能选择此选项.您只生成一封电子邮件,让邮件服务器处理向每个收件人发送副本的繁重工作.