2011-11-25 214 views
1

我試圖在PHP中發送HTML電子郵件,但它始終顯示電子郵件程序中電子郵件的源代碼。但它應該將html電子郵件呈現爲html,而不是將源代碼顯示爲電子郵件內容。嘗試使用sendmail發送/發送html電子郵件,但顯示電子郵件的源代碼

我把我的郵件是這樣的:

$fd = popen("/var/qmail/bin/sendmail -t","w") or die("Couldn't Open Sendmail"); 
    fputs($fd, "To: ".$to2." \n"); 
    fputs($fd, "From: \"Test <[email protected]>\" \n"); 
    fputs($fd, "Subject: ".$subject." \n"); 
    fputs($fd, "X-Mailer: PHP5 \n"); 
    fputs($fd, "Mime-Version: 1.0 \n"); 
    fputs($fd, " \n"); 
    fputs($fd, "--".$mime_boundary.""); 
    fputs($fd, "Content-Type: text/html; charset=\"utf-8\"; boundary=\"".$mime_boundary."\" \n"); 
    fputs($fd, "Content-Transfer-Encoding: quoted-printable \n"); 
    fputs($fd, " \n"); 
    fputs($fd, $sendmail_body." \n"); 
    fputs($fd, "".$mime_boundary."--"); 
    pclose($fd); 

的HTML文件的內容是這樣的:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="de" lang="de"> 
<head> 
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 
<title>Test</title> 
<style type="text/css"> 
body { font: normal 12px Verdana, Arial, Helvetica, sans-serif; } 
</style> 
</head> 
<body> 
</body> 
</html> 

現在的工作:

$ FD = POPEN( 「/ var/qmail/bin/sendmail -t」,「w」)或死(「無法打開 Sendmail」); fputs($ fd,「To:」。$ to1。「\ n」); fputs($ fd,「From: \」Test \「\ n」); fputs($ fd,「Subject:」。$ subject。「\ n」); fputs($ fd,「X-Mailer: PHP5 \ n」); fputs($ fd,「Mime-Version:1.0 \ n」); fputs($ fd,「Content-Type:multipart/alternative; boundary = \」「。$ mime_boundary。」\「\ n」); fputs($ fd,「\ n」); fds($ fd,「 - 」。$ mime_boundary。「\ n」); fputs($ fd,「Content-Type: text/html; charset = \」utf-8 \「\ n」); fputs($ fd, 「Content-Transfer-Encoding:quoted-printable \ n」); fputs($ fd,「 \ n」); fputs($ fd,$ sendmail_body。「\ n」); fputs($ fd, 「 - 」。$ mime_boundary。「 - \ n」); pclose函數($ FD);

而我的html文件的第一行是空的,或者我在html內容前添加一個\ n。

+0

不得不編輯我的問題,因爲我忘了一些行 –

回答

2

我想你應該考慮派遣多,因爲一些客戶端不支持HTML郵件或只是喜歡純文本:

$headers = "From: Example <[email protected]>\r\n 
    MIME-Version: 1.0\r\n 
    Content-Type: multipart/alternative; boundary={$mime_boundary}\r\n 
    X-Mailer: PHP5"; 

$message = "This is a MIME-Message. If you can read this your client does not support the MIME format.\r\n 
\r\n 
{$mime_boundary}\r\n 
Content-Transfer-Encoding: quoted-printable\r\n 
Content-Type: text/plain; charset=utf8;\r\n 
\r\n 
Text Content encoded in quoted printable 
\r\n 
\r\n 
{$mime_boundary}\r\n 
Content-Transfer-Encoding: quoted-printable\r\n 
Content-Type: text/html;charset=utf8;\r\n 
\r\n 
HTML Content encoded in quoted printable 
\r\n 
--{$mime_boundary}"; 

mail($to, $subject, $message, $headers); 

只要在php.ini中正確配置sendmail路徑和參數,這將通過sendmail以multipart/alternative類型發送郵件。

+0

\ n或\ r \ n?你使我感到困惑 –

+0

\ n是類似unix的方式,windows使用\ r \ n和Mac OS只是\ r - 所以\ r \ n會在每個系統上產生換行符... \ r \ n或\ n不應該在郵件上下文有所作爲,但有時他們這樣做... –

+0

其在Linux上運行的UNIX機器/網絡服務器,所以我不neet \ r?有時?所以我應該使用\ r \ n? –

0
fputs($fd, "X-Mailer: PHP5 \n\n"); 

嘗試刪除第二個\ n,因爲它是標頭終止的標誌。

+0

然後它的工作? –

1

這爲我工作:

 
<?php 
$message=<<<EOL 
--frontier 
Content-type: text/plain; charset=UTF-8 
Content-Transfer-Encoding: 7bit 
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="de" lang="de"> 
<head> 
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 
<title>Test</title> 
<style type="text/css"> 
body { font: normal 12px Verdana, Arial, Helvetica, sans-serif; } 
</style> 
</head> 
<body> 
</body> 
</html> 
--frontier-- 
EOL; 

$fd = popen("/var/qmail/bin/sendmail -t","w") or die("Couldn't Open Sendmail"); 

fputs($fd, "To: ".$to." \n"); 
fputs($fd, "From: \"Example\" <[email protected]> \n"); 
fputs($fd, "Subject: ".$subject." \n"); 

fputs($fd,"MIME-Version: 1.0\n"); 
fputs($fd,"Content-type: multipart/alternative; boundary=\"frontier\"\n\n"); 
fputs($fd,"This is a message with multiple parts in MIME format.\n"); 

fputs($fd, $message); 
pclose($fd); 
?> 

我希望這將是有益的

+0

和我的doctype的HTML文件?我應該改變所有這些東西,如Transfer-Encoding,Content-Type,... –

+0

嘗試過它(我在html文件中使用doctype),但收到的郵件是空的(outlook,甚至沒有sourcecode存在於電子郵件中平原空),並在iPhone上顯示源代碼 –