How to handle multi-recipient emails

Hey everyone!

I made a blog site where users can go to and enter their email addresses. When a new blog is created an email is sent out to each recipient. I’m wondering what the best practice is for this.

As I have it now, 1 email is sent out per recipient (ex: there are 6 emails in the DB, 6 emails are sent out individually). Initially, however, I was attemping to just send 1 email out with multiple BCC recipients.

Which is better? I couldn’t actually get the BCC thing to work as the $headers variable couldn’t accept a while statement but, then, I didn’t give it a fair chance. Here are simplified versions of both ideas:

1 email per recipient:


$sql = "SELECT Email FROM subscribe";
$RS = mysql_query($sql, $oConn);
$numEmails = mysql_num_rows($RS);

//Check how many emails were successful
$sent = 0;

//Begin email specs

while($row = mysql_fetch_array($RS)){

    $email = $row['Email'];

     $to = $email;
     $subject = 'This email is unique to you';
     $headers = 'From: emailer@mysite.com';
     $body = 'This is a plain text email about nothing';

     if(mail($to, $subject, $message, $headers)){
          $sent++;
     }else{
          $sent = $sent;
     }

}

echo $sent . " out of " . $numEmails . " were sent";

1 email, many recipients (this is theory as it doesn’t work)


$sql = "SELECT Email FROM subscribe";
$RS = mysql_query($sql, $oConn);

//Begin email specs:

$to = 'somebody@domain.com';
$subject = 'This email is unique to you';
$headers = 'From: emailer@mysite.com' . '\\r\
';
//Yes this syntax is painful but the idea is there:
$headers .= 'BCC: ' . 
     while($row = mysql_fetch_array($RS)){ 
          echo $row['Email'];     
     }
$body = 'This is a plain text email about nothing';

if(mail($to, $subject, $message, $headers)){
    echo "Successful";
}else{
    echo "Unsuccessful";
}

The trick to this really becomes once you hit the limit on header size and/or BCC recipients your mail daemon will allow; If you have 5000 BCC recipients, most mail daemons are going to deny sending your mail. (Postfix i believe has a default limit of 1000), so you might need to do some investigating on what you can and cant do before you proceed with a code strategy.

What solution is best might depend on the number of subscribers, and the limits imposed by your host regarding max number of mails etc.

Anyway, the second solution should be something like


$bcc = array();
while ($row = mysql_fetch_array($RS)) {            
  $bcc[] = $row['Email'];
} 
$headers .= 'BCC: ' . implode(',', $bcc) . "\\r\
";

Yes that makes total sense.

I’m actually not expecting a lot of subscribers (maybe 50). It’s a blog I made for my wife who’s pregnant and wants something to write out the interesting happenings of the baby’s life so that our friends and relatives can stay in the loop.

Having said that, knowing now that the number of subscribers is going to be very low, you guys are saying that it would be more efficient to go the BCC route?

Also, guido2004, thanks for offering that syntax – you’ve probably saved me hours of work!!!

If your subscribers are going to be that low, I would say BCC is the way to go, yes.