PHP Sending mass mails: One for each or one for all?
When sending mass mails with PHP, is it better to send each subscriber an e-mail (running 开发者_运维技巧a for loop through all the e-mail addresses) or is it better to just add all in BCC in a comma separated list and thus sending only one e-mail?
Thank you.
There's a good chance the number of addresses in the BCC field is limited on the SMTP server (to avoid spamming). I'd go with the safe route and send an e-mail to each individual subscriber. That will also allow you to customize the e-mail for each subscriber if needed.
Also note that mail() is probably not the best way to send bulk mail (due to the fact that it opens a new connection to the SMTP server each time it's invoked). You may want to look into PEAR::Mail.
Best practice is to send an email per recipient.
If it's a linux mail server, it can handle massive throughputs so volume should not be an issue unless it's a crap server!
If it's a shared webserver your host may not be happy - if this si the case I'd split it into chuncks and spread the send. If it's dedicated then do as you will :)
If the sending process for some reason failed (example cause might me unresolvable domain) for one of the BCC recipients, the whole operation would be canceled (which is in 99% of cases unwanted behavior).
I you send the emails in a PHP loop, even if one of the emails fails to send, other emails will be sent.
As the others says one mail per recipient is the better fit.
If you want a library to do the dirty job for you, give a try to SwiftMailer http://swiftmailer.org
Here is an example directly from the docs:
require_once 'lib/swift_required.php';
//Create the Transport
$transport = Swift_SmtpTransport::newInstance('localhost', 25);
//Create the Mailer using your created Transport
$mailer = Swift_Mailer::newInstance($transport);
//Create a message
$message = Swift_Message::newInstance('Wonderful Subject')
->setFrom(array('john@doe.com' => 'John Doe'))
->setTo(array('receiver@domain.org', 'other@domain.org' => 'A name'))
->setBody('Here is the message itself')
;
//Send the message
$numSent = $mailer->batchSend($message);
printf("Sent %d messages\n", $numSent);
/* Note that often that only the boolean equivalent of the
return value is of concern (zero indicates FALSE)
if ($mailer->batchSend($message))
{
echo "Sent\n";
}
else
{
echo "Failed\n";
}
*/
It also has a nice Antiflood plugin: http://swiftmailer.org/docs/antiflood-plugin-howto
精彩评论