我正在使用函數發送UTF8電子郵件。 由於至少有一個收件人與我的函數發送的電子郵件問題(不是UTF8,所以特殊字符打破;一些郵件標題出現在正文中),我想知道是否必須在標題字符串的末尾放置另一個換行符:郵件()頭必須用換行符結束嗎? (IncrediMail問題)
function mail_utf8($to, $subject = "(No subject)", $message = "", $header = "")
{
$header_ = 'MIME-Version: 1.0'."\r\n".'Content-type: text/plain; charset=UTF-8'."\r\n";
$header_ .= "From: [email protected]"; //Should be optional...
if (!empty($header)) $header_ .= "\r\n".$header;
$out = mail($to, '=?UTF-8?B?'.base64_encode($subject).'?=', $message, $header_);
return $out;
}
那麼,什麼是正確的 - mail_utf8("[email protected]", "", "blah", "")
或mail_utf8("[email protected]", "", "blah", "\r\n")
?
編輯:這種問題的顯而易見的資源 - www.php.net/mail - 僅在示例#4中使用了這種換行符,與#2中不同。
編輯2:所以這裏是當前版本。查看更多信息的評論。
function mail_utf8($to, $subject = "(No subject)", $message = "", $header = "")
{
$linebreak = PHP_EOL; //Seems to work everywhere, including IncrediMail
$linebreak = "\n"; //Debug
$header_ = 'MIME-Version: 1.0'.$linebreak.'Content-type: text/plain; charset=UTF-8'.$linebreak;
$header_ .= "From: [email protected]".$linebreak;
if (!empty($header)) $header_ .= $header.$linebreak; //Last line break !?
$out = mail($to, '=?UTF-8?B?'.base64_encode($subject).'?=', $message, $header_);
return $out;
}
那麼你正在使用無論是在你的代碼。 –
「*部分郵件標題出現在主體*中*」部分讓我認爲您的標題中可能有*空行*,就像'「Content-type:text/plain; charset = UTF-8 \ r \ n \ r \ n另一個標題:值「'。我會將'\ r \ n'追加到'From:'行,並確保'$ headers'參數不以'\ r \ n'開頭。 – binaryLV
@binaryLV:正如我在下面寫的,似乎IncrediMail(電子郵件客戶端 - 非郵件服務器)在「MIME-Version:1.0」之後產生了第一個雙重換行符,這可以解釋爲什麼「Content-Type:...」是第一條身體線(僅在IncrediMail中)。但是通過使用「\ n」而不是「\ r \ n」,我不符合標準,對吧? – basic6