我看到:
Content-typetext/html; charset=iso-8859-1
,我應該看到:
Content-type: text/html; charset=iso-8859-1
那是一個剪切/粘貼錯誤?在你的代碼中,還是在你的結果中?因爲只有HTML的郵件經常會得到很高的垃圾郵件分數(使用SpamAssassin,SpamBouncer等),因此您可能希望在多部分郵件中包含文本/純文本和文本/ html類型。
更新#1:
看來,\r\n
被解釋爲兩個新行而不是一個。不同的平臺在實施SMTP時可能會有不同的錯誤。您可能會將您的線路結局更改爲\n
。如果它適用於你的系統,不要依賴它,因爲它可能不適用於不同的系統。
此外,請考慮切換到另一種發送郵件的方法。推薦使用phpMailer和SwiftMailer,而不是PHP的內部mail()
函數。
更新#2:
大廈無痕的建議,這裏的代碼,你可以用它來組織你的頭更好,以及創建一個明文部分:
filter_var($to, FILTER_VALIDATE_EMAIL) or die("Invalid To address");
filter_var($email, FILTER_VALIDATE_EMAIL) or die("Invalid From address");
$subject = 'HTML e-mail test';
$messagetext = 'TEST in TEXT';
$messagehtml = '<html>
<body>
<h1>TEST</h1>
<p>in HTML</p>
</body>
</html>';
// We don't need real randomness here, it's just a MIME boundary.
$boundary="_boundary_" . str_shuffle(md5(time()));
// An array of headers. Note that it's up to YOU to insure they are correct.
// Personally, I don't care whether they're a string or an imploded array,
// as long as you do input validation.
$headers=array(
'From: <' . $email . '>',
'MIME-Version: 1.0',
'Content-type: multipart/alternative; boundary="' . $boundary . '"',
);
// Each MIME section fits in $sectionfmt.
$sectionfmt = "--" . $boundary . "\r\n"
. "Content-type: text/%s; charset=iso-8859-1\r\n"
. "Content-Transfer-Encoding: quoted-printable\r\n\r\n"
. "%s\n";
$body = "This is a multipart message.\r\n\r\n"
. sprintf($sectionfmt, "html", $messagehtml)
. sprintf($sectionfmt, "plain", $messagetext)
. "--" . $boundary . "--\r\n";
$mailb = mail($to, $subject, $body, implode("\r\n", $headers));
我不是說這是以任何方式做到這一點的正確方法,但是如果策略吸引你,並且你認爲在沒有查看它6或12個月(即乍一看有意義)之後可以保持這樣的代碼,然後隨意使用或適應它。
聲明:這是未經測試的,有時我會犯錯誤......只是爲了讓人們保持腳趾頭。 :)
請使用一個數組,甚至nowdoc語法來處理像這樣的長blob。 http://www.php.net/manual/en/language.types.string.php#language.types.string.syntax.nowdoc – Incognito
@Incognito - mail()中的標題是字符串,而不是數組,並且正式應該有以'\ r \ n'結尾的行(不僅僅是系統的換行符)。除非你正在想'implode()'在飛行中嗎?你會如何確保nowdoc具有正確的行結尾? – ghoti
@ghoti我用'。='來表達他的字符串構建操作。該數組將提供一個有意義的抽象,至少可以包含邏輯幫助防止像'TO:'字段被提供的事情'[email protected] \ r \ n到:[email protected] \ r \ n \ r \ nSpamSpamSpam \ r \ n。\ r \ n \ r \ N'。 - 此外,只需使用像您在下面建議的現有解決方案是首選。 – Incognito