Although I expect this article to cause a few ruffled feathers amongst the programming community (since most of them are against HTML email), there are times when the client will ask for it anyway, so you have to know how to do it. We had run into difficulty finding a straight answer on this topic, and many of the articles we had found on it gave us bizarre results in the HTML mail… so once we figured out what worked, we decided to post it.

We have tested this on several different email clients (including but not limited to: Hotmail, Yahoo, AOL, Outlook Express, Outlook, and various text-only email clients such as Mutt).

In the email clients that render HTML (ie – not text-only) the only real difference we found was that Outlook (regular Outlook, not Express) showed the background color from the BODY tags, but not the background image. Yahoo and Hotmail stripped out the background color and images, but if your HTML mail is designed with this in mind, you can easily design around that.

Make sure the email will still look nice if the background color and/or images are stripped out of it.

Obviously, you would change the email addresses and messages to fit your own needs.

NOTE: PEAR has some libraries that make this even easier. (This article is old.)

<?php
/* ---------------------------------------------- */
/* ------------ BEGIN PHP SNIPPET ----------------*/
/* ---------------------------------------------- */

$headers .= “FROM: invites@yourbigevents.com\n”;
$headers .= “Reply-To: invites@yourbigevents.com\n”;

// This is the important part!
// This content type identifies the content of the message.
// The boundary delimits the plain text and html sections.
// The value of the boundary can be anything – you can even
// use the same one we used here
$headers .= “Content-Type: multipart/alternative; boundary=\”—-=_NextPart_000_002C_01BFABBF.4A7D6BA0\”\n\n”;

// Now begin your message, starting with the delimiter we specified in the boundary
// Notice that two extra dashes (–) are added to the delimiters when
// They are actually being used.
$message = ‘——=_NextPart_000_002C_01BFABBF.4A7D6BA0
Content-Type: text/plain; charset=us-ascii
Content-Transfer-Encoding: 7bit

Your plaintext email content here.’;

// Now begin your HTML message, starting with the delimiter
// Also notice that we add another content-type line which
// lets the mail client know to render it in HTML
$message .= ‘——=_NextPart_000_002C_01BFABBF.4A7D6BA0
Content-Type: text/html; charset=us-ascii
Content-Transfer-Encoding: 7bit

Your HTML email here

——=_NextPart_000_002C_01BFABBF.4A7D6BA0–‘;

// Now send the mail.
// The additional header, “-f invites@yourbigevents.com” is
// only required by certain server configurations.
mail($v, “2002 Winter Games Invitation”, $message ,$headers,“-f invites@yourbigevents.com”);
?>

ssd-virtual-servers-banner-468x60

Advertisement

468x60_makemoney
Previous post

Displaying file sizes in human readable format

Next post

Dynamic thumbnailing with PHP and Imagemagick

snipe

snipe

I’m a tech geek/dev/infosec-nerd/scuba diver/blacksmith/sword-fighter/crime fighter/ENTP/warcrafter/activist. I'm the CTO at Mass Mosaic and the CEO of Grokability, Inc. in San Diego, CA. Tweet at me @snipeyhead or read more...