Reputation: 14250
I'm using the PHP native mail()
function to send HTML emails and have a formatting problem in the users most common email client - Outlook 2007 (in addition to some other email clients) - all the html tags are exposed so it looks like gibberish to a non-web-developer.
I'm sending HTML email the same way that the PHP manual demos it. Example:
$message = get_HTML_email_with_valid_formatting();
$headers = "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: text/html; charset=UTF-8\r\n";
$headers .= "From: example.com <[email protected]>\r\n";
$headers .= "Reply-To: [email protected]\r\n";
mail('[email protected]', 'test', $message, $headers);
Because testing various email clients is hard, I have signed up with http://litmusapp.com/ so I can see a screenshot of the emails in 47 different email clients. Most are ok (e.g. gmail, thunderbird, Lotus Notes) but all the different versions of Outlook are not ok.
To fix the formatting issue I had do the following:
$headers = "MIME-Version: 1.0\r\n";
mail header. \n
" instead of "\r\n
".Does anyone know why I am getting better results with HTML emails when I do not conform to the manual?
Info:
Upvotes: 2
Views: 8375
Reputation: 41
If You use postfix<2.9, You can just put sendmail_path = "tr -d '\r'|sendmail -t -i"
into php.ini.
Upvotes: 4
Reputation: 2440
You have 2 solutions:
I have a VMware image with a LAMP stack. In order to send email, I finally decided to:
For the sendmail part, you can follow this: http://www.geoffke.be/nieuws/13/
IMPORTANT: Some webhosters may use only stable packages which means you can have... a Postfix older than 2.9!!! Exemple: http://packages.debian.org/search?keywords=postfix
Upvotes: 2
Reputation: 14250
I suspect it is my version of Postfix - version 2.3.3 is 5 years old and perhaps it is converting LF to CRLF but seeing as I had CRLF already, I think I was sending CRCRLF to the mail clients.
Unfortunately, I'm not in the situation to upgrade Postfix. So for the moment I have converted the code to use a configurable variable for the line endings so that it is easy to change in the future:
$eol = "\n";
$message = get_HTML_email_with_valid_formatting();
$headers = "MIME-Version: 1.0".$eol;
$headers .= "Content-Type: text/html; charset=UTF-8".$eol;
$headers .= "From: example.com <[email protected]>".$eol;
$headers .= "Reply-To: [email protected]".$eol;
mail('[email protected]', 'test', $message, $headers);
Upvotes: 0
Reputation: 5766
The email is been interpreted as text/plain instead of intended html. The reason for this is that text/html is a multipart subtype thus requiring boundary declarations.
Your code is missing a the header boundary declaration:
$message = get_HTML_email_with_valid_formatting();
$headers = "MIME-Version: 1.0\r\n";
$headers .= "--$boundary\r\n"."Content-Type: text/html; charset=ISO-8859-1\r\n";
$headers .= "From: example.com <[email protected]>\r\n";
$headers .= "Reply-To: [email protected]\r\n";
mail('[email protected]', 'test', $message, $headers);
Check this wiki about MIME & Multipart Messages: http://en.wikipedia.org/wiki/MIME#Multipart_messages
Upvotes: -1