【问题标题】:PHP text/html multipart email appears as raw text in hotmailPHP 文本/html 多部分电子邮件在 hotmail 中显示为原始文本
【发布时间】:2012-03-25 22:58:26
【问题描述】:

我正在使用 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);

我做错了什么?

【问题讨论】:

  • 如果您不想费心阅读所有 RFC - 为什么不使用可以发送格式正确且正确的电子邮件的库(如 phpmailer)?

标签: php email multipart


【解决方案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."--";

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

【讨论】:

  • hmm .. 添加了换行符 .. 仍然是同样的问题。可能会给PHPMailer一个尝试。谢谢
  • 我的主机也在运行 PHP 4.4.7。我想知道这是否会成为一个问题。
  • @Shahid 再看一遍,我还看到$plain_text 部分之后只有一个换行符,它是字符串$plain_text 的一部分。我在$plain_text 之后的--$mime_boundary 之前添加了另一个换行符。这可能是原因......另外,需要在$html_text 之后添加两个换行符,然后再最后的--$mime_boundary--
  • @Michael.. 谢谢.. 但是即使添加了所有这些换行符,问题仍然存在
猜你喜欢
  • 2015-06-09
  • 1970-01-01
  • 1970-01-01
  • 2014-10-22
  • 2011-09-24
  • 1970-01-01
  • 1970-01-01
  • 2013-07-13
  • 1970-01-01
相关资源
最近更新 更多