2012-03-25 91 views
1

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

$cname = "test"; 
$to = "[email protected]"; 
$autoReplyTo = "[email protected]"; 
$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); 

我在做什么错?

+2

如果你不想读取所有的RFC - 为什么不使用库(像phpmailer),可以发送一个格式正确的电子邮件? – zerkms 2012-03-25 23:03:56

+0

看看这里:http://stackoverflow.com/a/9860369/1104695 – guybennet 2012-03-25 23:06:24

回答

1

这可能是也可能不是唯一的问题,而是你缺少的Content-Type头平原和HTML的章节后,换行:

$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消息,很多我们这里的SO会推荐一个像PHPMailer这样的邮件库,它可以更轻松地处理这个问题。手动构建消息往往容易出错,并且受到SMTP服务器实现之间的不一致性或本机平台换行符之间的差异的影响。

+0

嗯..添加换行符仍然是同样的问题。会给PHPMailer一个尝试可能。谢谢 – Shahid 2012-03-25 23:17:10

+0

我的主机也运行PHP 4.4.7。我想知道这是否会成为一个问题。 – Shahid 2012-03-25 23:50:47

+0

@Shahid再看一遍,我还看到'$ plain_text'部分后面只有一个换行符,它是字符串'$ plain_text'的一部分。我在'$ plain_text'后面的' - $ mime_boundary'之前添加了另一个linebreak。这可能是原因......此外,需要在最后一个' - $ mime_boundary - ' – 2012-03-26 01:31:12

相关问题