Snipe.Net Geeky, sweary things.

Sending HTML/Plain Text Mail Simultaneously using PHP

S

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”);
?>

About the author

snipe

I'm a tech nerd from NY/CA now living in Lisbon, Portugal. I run Grokability, Inc, and run several open source projects, including Snipe-IT Asset Management. Tweet at me @snipeyhead, skeet me at @snipe.lol, or read more...

By snipe
Snipe.Net Geeky, sweary things.

About Me

I'm a tech nerd from NY/CA now living in Lisbon, Portugal. I run Grokability, Inc, and run several open source projects, including Snipe-IT Asset Management. Tweet at me @snipeyhead, skeet me at @snipe.lol, or read more...

Get in Touch