941,498 Members | Top Members by Rank

Ad:
  • PHP Discussion Thread
  • Unsolved
  • Views: 53527
  • PHP RSS
Feb 7th, 2004
0

Sending HTML/Plain text Emails

Expand Post »
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?

[php]
$boundary = "nextPart";

$headers = "FROM: me@fromme.com\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);
[/php]
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
lunac is offline Offline
2 posts
since Feb 2004
Feb 7th, 2004
0

Re: Sending HTML/Plain text Emails

Success!

Those \r\n lines are SO picky.

This is what I got to work
[php]
$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);
[/php]

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.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
lunac is offline Offline
2 posts
since Feb 2004
Apr 3rd, 2008
0

Re: Sending HTML/Plain text Emails

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>
Last edited by peter_budo; Apr 5th, 2008 at 1:26 pm. Reason: Keep It Spam-Free - Do not spam, advertise, plug your website, or engage in any other type of self promotion.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
joebaileyorg is offline Offline
2 posts
since Apr 2008
Apr 4th, 2008
0

Re: Sending HTML/Plain text Emails

maybe because your server is on the black list ...
Reputation Points: 10
Solved Threads: 15
Junior Poster in Training
silviuks is offline Offline
95 posts
since Apr 2006
Apr 4th, 2008
0

Re: Sending HTML/Plain text Emails

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
Reputation Points: 10
Solved Threads: 0
Newbie Poster
joebaileyorg is offline Offline
2 posts
since Apr 2008
Jul 15th, 2011
0
Re: Sending HTML/Plain text Emails
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?
Reputation Points: 10
Solved Threads: 0
Newbie Poster
pirco is offline Offline
1 posts
since Jul 2011

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in PHP Forum Timeline: phpmyadmin exported sql errors with mysql_query
Next Thread in PHP Forum Timeline: FFDB only shows 1st record





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC