I'm trying to send an html/plain text email for an invoice program I am writing. I really want the html message because it offers better layout options for invoicing and some features to help my customers with their payments. However, some email clients don't work with html email -- especially web mail. So I need the alternative plain text email in those cases.

I found this script at http://snipe.net/geek/toolz/htmlmail.php. It's similar to a script at http://www.zend.com/zend/trick/html-email.php. I think the main difference is how they encode the message. I found the message encoding in the zend article doesn't work for me at all. The snipe script was only sending one email with all the headers in the email.

I played around with the line breaks and was able to finally get two emails (plain/html). I can switch between and view each one. However the header information for each type is still showing in the email.

Does anyone know what I'm doing wrong?

$boundary = "nextPart";

$headers = "FROM: [email]me@fromme.com[/email]\r\n";
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: multipart/alternative; boundary = $boundary\r\n\r\n";
$headers .= "This is a MIME encoded message.\r\n\r\n";

//text version
$headers .= "--$boundary\n
			Content-Type: text/plain; charset=ISO_8859-1\r\n
			Content-Transfer_Encoding: 7bit\r\n\r\n";
$headers .= "This is the plain version\r\n\r\n";

// html version
$headers .= "--$boundary\r\n
			Content-Type: text/html; charset=ISO_8859-1\r\n
			Content-Transfer_Encoding: 7bit\r\n\r\n";
$headers .= "This is the <b>HTML</b> version";

mail("me@myemail.com", "An HTML Message", "", $headers);

Recommended Answers

All 5 Replies

Success!

Those \r\n lines are SO picky.

This is what I got to work

$boundary = "nextPart";

$headers = "MIME-Version: 1.0\r\n";
$headers .= "From: Me <sales@mysite.com>\r\n";
$headers .= "Content-Type: multipart/alternative; boundary = $boundary\r\n";

//text version
$headers .= "\n--$boundary\n"; // beginning \n added to separate previous content
$headers .= "Content-type: text/plain; charset=iso-8859-1\r\n";
$headers .= "This is the plain version";

//html version
$headers .= "\n--$boundary\n";
$headers .= "Content-type: text/html; charset=iso-8859-1\r\n";
$headers .= "This is the <b>HTML</b> version";

mail("me@mymail@mac.com", "An HTML Message", "", $headers);

Hopes this helps anyone else who's had problems with html/plain email.

Additional note:
The email message that I send (both versions) contained ":" . This caused my email not to show up in Mac OS X.3 Mail.app, although it showed properly in Entourage. Maybe it was because I sent my message in the headers part -- I'm not sure. Anyway, removing the ":" fixed that problem too.

Lunac,

Thanks. I'm on a project now and the code fit nicely in.

Only issue I had, that doesn't relate to you, but Outlook 2007 is now sending HTML files to the junk folder if they have images.
<snipped>

maybe because your server is on the black list ...

Good point. I worked on testing it a lot last night. From what I can tell, the site isn't black listed, but had more to do with the content. I haven't quite resolved everything yet, but here's what I have:

Email 1
Subject: Contact from x website
-result: non-spam
Body: contains a small amount of html, not much really just contact info
-result: non-spam
-If I add a graphic, no effect
-If I add a graphic and put a link around it, it is considered spam.

Email 2:
Subject: x wants you to know about this promotion
-result: spam
Body: not much just a graphic for now and minimul html
-result: spam all around
delivers in junk mail

late reply.. but what if I wanted to send different message body – HTML formatted for the HTML version and plain text body for the plain text version?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.