I made an HTML e-mail send with PHP, but my client receives this as pure code. Here is the PHP code to send the mail:
$subject = 'HTML e-mail test';
$message = '<html>
<body>
<h1>TEST</h1>
</body>
</html>';
$headers = "MIME-Version: 1.0\r\n";
$headers .= "Content-type: text/html; charset=iso-8859-1\r\n";
$headers .= "To: <".$to.">\r\n";
$headers .= "From: <".$email.">\r\n";
$mailb = mail($to, $subject, $message, $headers);
It works fine for me, but they receive it as:
Content-type: text/html; charset=iso-8859-1
To: <[email protected]>
From: <[email protected]>
<html>
<body>
<h1>TEST</h1>
</body>
</html>
Is there something wrong with my headers? Or is it their Outlook?
How can I fix this problem?
Thanks in advance!
I see:
Content-typetext/html; charset=iso-8859-1
where I should see:
Content-type: text/html; charset=iso-8859-1
Is that a cut/paste error? In your code, or in your result?
Note that you probably want to include both text/plain and text/html types in a multipart message, since HTML-only mail often gets high spam scores (using SpamAssassin, SpamBouncer, etc).
UPDATE #1:
It appears that the \r\n
is being interpreted as two newlines instead of one. Different platforms may have different bugs in their implementation of SMTP. It's possible that you can get away with changing your line endings to just \n
. If that works with your system, don't rely on it, as it may not work on a different system.
Also, consider switching to a different method for sending mail. phpMailer and SwiftMailer are both recommended over PHP's internal mail()
function.
UPDATE #2:
Building on Incognito's suggestion, here's code you might use to organize your headers better, as well as create a plaintext part:
filter_var($to, FILTER_VALIDATE_EMAIL) or die("Invalid To address");
filter_var($email, FILTER_VALIDATE_EMAIL) or die("Invalid From address");
$subject = 'HTML e-mail test';
$messagetext = 'TEST in TEXT';
$messagehtml = '<html>
<body>
<h1>TEST</h1>
<p>in HTML</p>
</body>
</html>';
// We don't need real randomness here, it's just a MIME boundary.
$boundary="_boundary_" . str_shuffle(md5(time()));
// An array of headers. Note that it's up to YOU to insure they are correct.
// Personally, I don't care whether they're a string or an imploded array,
// as long as you do input validation.
$headers=array(
'From: <' . $email . '>',
'MIME-Version: 1.0',
'Content-type: multipart/alternative; boundary="' . $boundary . '"',
);
// Each MIME section fits in $sectionfmt.
$sectionfmt = "--" . $boundary . "\r\n"
. "Content-type: text/%s; charset=iso-8859-1\r\n"
. "Content-Transfer-Encoding: quoted-printable\r\n\r\n"
. "%s\n";
$body = "This is a multipart message.\r\n\r\n"
. sprintf($sectionfmt, "html", $messagehtml)
. sprintf($sectionfmt, "plain", $messagetext)
. "--" . $boundary . "--\r\n";
$mailb = mail($to, $subject, $body, implode("\r\n", $headers));
I'm not saying this is The Right Way to do this by any means, but if the strategy appeals to you, and you think you can maintain code like this after not having looked at it for 6 or 12 months (i.e. it makes sense at first glance), then feel free to use or adapt it.
Disclaimer: this is untested, and sometimes I make typos ... just to keep people on their toes. :)