PHP Sending mass mails: One for each or one for all?

607 Views Asked by At

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.

4

There are 4 best solutions below

1
On BEST ANSWER

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.

0
On

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 :)

2
On

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.

0
On

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('[email protected]' => 'John Doe'))
  ->setTo(array('[email protected]', '[email protected]' => '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