Not Spam

My friend is embarking on a tour to promote his new book. He’ll be on the road so email is going to be the best way to announce what’s going on to interested parties. However, sending email from his Yahoo! account to multiple recipients proved problematic: emails addressed To: a complete list of recipients raised some ire from people not wanting their emails revealed to other recipients.

The next attempt went a little better: he addressed it to himself and BCC’ed actual recipients. But so do spammers, and a lot of people never even saw this one. The alternative is to send the email over and over to each individual, which is even more work than the tedium of adding all email addresses to a BCC field with Yahoo!’s address book.

Obviously, this is something where a computer should be doing the heavy lifting, but the options out there are either not free, or contain intrusive ads. (Which, come to think of it, Yahoo! Mail also does.) Given the nature of the book, this is undesirable.

I wanted to help, so I threw together a little three-page web app to store email addresses and names, and loop over each item to send an individual email to every person. We also want to do the right thing by sending a MIME multi-part style email, so email clients do the right thing based on each user’s preferences. PHP’s mail() function, with some custom headers, to the rescue.

These lines set up the headers:

$to = "\"$rname\" <$remail>"; # recipient
$from = "\"$sname\" <$semail>"; # sender
$headers = 'From: ' . $from . "\n";
$headers .= 'To: ' . $to . "\n";
$headers .= 'Return-Path: ' . $from . "\n";
$headers .= 'Content-Type: multipart/alternative; boundary="'
$headers .= $body_simple . "\n";
$headers .= '--' . $boundary . "\n";
$headers .= 'Content-Type: text/plain; charset=ISO-8859-1' ."\n";
$headers .= 'Content-Transfer-Encoding: 8bit'. "\n\n";
$headers .= $body_plain . "\n";
$headers .= 'Content-Type: text/HTML; charset=ISO-8859-1' ."\n";
$headers .= $body_html . "\n";
$headers .= '--' . $boundary . "--\n";

Each section of a multi-part email is set off by a unique boundary string, defined in this case as follows:

$boundary = md5(uniqid(time()));


Now “abuse” mail() as follows:

mail('', $subject,'', $headers);
Even though ‘to’ and ‘content’ params are empty, the headers contain everything needed to properly deliver each message. So far, it works.
Lots of the code above is based on some sample code from
Cats: experiment
Tags: , , ,