Sending bulk mail using phpmailer
I am new to Phpmailer and I am using it to send a bulk Email to over a thousand people from a noreply account. The code works fine when I send the Email to one or two people but when I send it to everybody (including myself) it goes to spam. One more problem is in details of the Email it shows the Email ids of all the people to whom it was sent which I don't want it to do. The code is as follows:
//date_default_timezone_set('America/Toronto');
require_once('../class.phpmailer.php');
//include("class.smtp.php"); // optional, gets called fr开发者_如何学Goom 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
}
As JoLoCo points out, the AddAddress()
method just ADDS a new address to the existing recipient list. And since you're doing it as an add/send loop, you're sending out a helluva lot of duplicate copies to the first recipient, one less to the second, etc...
What you need is:
while($row = mysql_fetch_row(...)) {
$mail->AddAddress($row[0]);
$mail->send();
$mail->ClearAllRecipients(); // reset the `To:` list to empty
}
On the other hand, since this spams your mail server with a lot of single emails, another option is to generate one SINGLE email, and BCC all the recipients.
$mail->AddAddress('you@example.com'); // send the mail to yourself
while($row = mysql_fetch_row(...)) {
$mail->AddBCC($row[0]);
}
$mail->send();
This option is most likely preferable. You only generate a single email, and let the mail server handle the heavy duty work of sending out copies to each recipient.
I think you're adding the new address to the email already sent -- so the first email will go to one person, the second email sent will go to that same person plus another, the third one will go to those two plus one more, and so on.
Also, I don't think you need to set the AltBody and MsgHTML every time.
You should add all the addresses to the BCC field first, then send.
So try...
// rest of code first
$mail->AltBody = "To view the message, please use an HTML compatible email viewer!";
$mail->MsgHTML($body);
$mail->AddAddress("you@example.com")
$ids = mysql_query($select, $connection) or die(mysql_error());
while ($row = mysql_fetch_row($ids)) {
$mail->AddBCC($row[0]);
}
$mail->Send();//Sends the email
Use BCC (Blind Carbon Copy) to hide the list of recipients. Related to the spam problem, it depends on the email provider of the recipients what is going to spam, and what is not, and there are many factors out there.
精彩评论