2012-03-25 51 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

相關問題