PHP text / html多部分电子邮件在hotmail中显示为原始文本

时间:2012-03-25 22:58:26

标签: php email multipart

我正在使用PHP测试自动回复多部分电子邮件。问题是当我在hotmil中收到电子邮件时,整个内容(包括html和随机哈希)显示为原始文本。这是我的代码:

$cname = "test";
$to = "me@myemail.com";
$autoReplyTo = "me@myemail.com";
$autoReplySubject = "Enquiry";

$mime_boundary = md5(date('U'));

$autoReplyheaders = "From: XXXXX <" . $to . ">" . "\r\n" .
                    "MIME-Version: 1.0" . "\r\n" .
                    "Content-Type: multipart/alternative; boundary=$mime_boundary" .
                    "Content-Transfer-Encoding: 7bit". "\r\n";


$plain_text = "Dear " . $cname . ".\r\n";
$plain_text = "Thank you for contacting us.\r\n";
$plain_text .= "We are currently processing your query and will contact you shortly. We appreciate the time and interest you have shown in our company.\r\n";
$plain_text .= "Sales Team\r\n";
$plain_text .= "Note: This is an auto-generated email, please do not reply.\r\n";


$html_text = '<html><head><title>AUTO REPLY</title></head><body>';
$html_text .= '<p><img src="http://www.xxxxxx.xx/images/logo.png" /></p>';
$html_text .= '<p>Dear '.$cname.',<br />
    Thank you for contacting us.<br />
    We are currently processing your query and will contact you shortly.<br />
    We appreciate the time and interest you have shown in our company.</p>';

$html_text .= '<p><b>Sales Team</b></p>';
$html_text .= '<p><i>Note: This is an auto-generated email, please do not reply.</i></p>';
$html_text .= '</body></html>';


$autoReplyMessage = "Auto reply" . "\r\n\r\n".
"--" . $mime_boundary.
"Content-Type: text/plain; charset=\"iso-8859-1\"".
"Content-Transfer-Encoding: 7bit". "\r\n\r\n".
$plain_text.
"--" . $mime_boundary.
   "Content-Type: text/html; charset=\"iso-8859-1\"".
   "Content-Transfer-Encoding: 7bit". "\r\n\r\n".
$html_text.
"--".$mime_boundary."--";


mail($autoReplyTo, $autoReplySubject, $autoReplyMessage, $autoReplyheaders);

我做错了什么?

1 个答案:

答案 0 :(得分:1)

这可能是也可能不是唯一的问题,但您在普通和HTML部分的Content-Type标题后面缺少换行符:

$autoReplyMessage = "Auto reply" . "\r\n\r\n".
"--" . $mime_boundary.
"Content-Type: text/plain; charset=\"iso-8859-1\"\r\n".
// ----------------------------------------------^^^^^
// Added \r\n
"Content-Transfer-Encoding: 7bit". "\r\n\r\n".
$plain_text. "\r\n".
//---------^^^^^^^^^
// Added another linebreak before MIME boundary
"--" . $mime_boundary.
   "Content-Type: text/html; charset=\"iso-8859-1\"\r\n".
// ----------------------------------------------^^^^^
// Added \r\n
   "Content-Transfer-Encoding: 7bit". "\r\n\r\n".
$html_text."\r\n\r\n".
// ---------^^^^^^^^^^
"--".$mime_boundary."--";

不是尝试手动制作multipart / mime消息,我们这里的很多人都会推荐像PHPMailer这样的邮件库,它可以更轻松地处理这个问题。手动构建消息往往容易出错,并且受SMTP服务器实现之间的不一致或本机平台换行符之间的差异。