PHP Sending HTML Formatted Emails

Do you have a question? Post it now! No Registration Necessary.  Now with pictures!

Threaded View
Just so this is out there on the web, I battled the past 24 hours
about this. Background info... I developed a ecommerce website in PHP
4 on a shared linux hosting plan from GoDaddy and had the html
formatted emails sending as text/html and were going fine with limited
header information. Then we moved the site over to a Dedicated Linux
hosting plan. This time, it's PHP 5. Things are bound to not work as
expected moving to a new programming language.

So what worked in PHP 4:

$headers  = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";

Now needs to be (at least what worked for me) in PHP 5

$headers  = "MIME-Version: 1.0\n";
$headers .= "Content-type: text/html; charset=iso-8859-1\n";
$headers .= "X-Priority: 3\n";
$headers .= "X-MSMail-Priority: Normal\n";
$headers .= "X-Mailer: php\n";

For the life of me I could not figure out why text/html formatted
emails were always sending as plain text and all the information was
shown in the body text using the PHP 4 method.

There... I hope I have saved someone else the trouble of losing the
amount of hair I did over this.


Re: PHP Sending HTML Formatted Emails


on 03/18/2007 11:24 PM said the following:
Quoted text here. Click to load it

I think the problem has nothing to do with the version but rather with
the MTA that used in the servers you tried. Depending on the MTA, the
line breaks may be processed differently.

Anyway, sending HTML messages like that you will have also message
filtering problems. You cannot send messages with text/html has main
content-type. That is not the way common mail programs will send HTML
formatted messages. Many systems will discard messages composed like
that because it is a sympthom that you are using a bulk mailing script
that may be spam.

What you need to do is to compose a multipart/alternative message that
encloses a text/plain part and an text/html part. If you do not know how
to do that, you can try many of the ready to use classes that can
compose multipart/alternative messages. I use this MIME message class:


Manuel Lemos

Metastorage - Data object relational mapping layer generator

PHP Classes - Free ready to use OOP components written in PHP

Site Timeline