簡體   English   中英

Outlook接收PHP HTML電子郵件作為代碼?

[英]Outlook receives PHP HTML e-mail as code?

我用PHP發送了一封HTML電子郵件,但我的客戶端將其作為純代碼收到。 這是發送郵件的PHP代碼:

$subject = 'HTML e-mail test';
$message = '<html>
    <body>
        <h1>TEST</h1>
    </body>
</html>';
$headers = "MIME-Version: 1.0\r\n"; 
$headers .= "Content-type: text/html; charset=iso-8859-1\r\n";
$headers .= "To: <".$to.">\r\n"; 
$headers .= "From: <".$email.">\r\n";

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

它適用於我,但他們收到它:

Content-type: text/html; charset=iso-8859-1

To: <email@email.com>
From: <email@email.com>

<html>
    <body>
        <h1>TEST</h1>
    </body>
</html>

我的標題有問題嗎? 或者是他們的Outlook 我該如何解決這個問題?
提前致謝!

我知道了:

Content-typetext/html; charset=iso-8859-1

在哪里我應該看到:

Content-type: text/html; charset=iso-8859-1

這是剪切/粘貼錯誤嗎? 在您的代碼中,還是在您的結果中?

請注意,你可能想在一個多部分消息文字 / plain和text / HTML類型,因為HTML的唯一的郵件經常獲得高分數的垃圾郵件(使用的SpamAssassin,SpamBouncer等)。

更新#1:

似乎\\r\\n被解釋為兩個換行符而不是一個換行符。 不同的平台在實現SMTP時可能會有不同的錯誤。 您可以將線路結尾更改為\\n 如果這適用於您的系統,請不要依賴它,因為它可能無法在其他系統上運行。

另外,請考慮切換到另一種發送郵件的方法。 phpMailerSwiftMailer都推薦使用PHP的內部mail()函數。

更新#2:

基於Incognito的建議,這里可以使用代碼更好地組織標題,以及創建明文部分:

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個月之后維護這樣的代碼(即它有意義乍一看),然后隨意使用或改編它。

免責聲明:這是未經測試的,有時候我會犯錯誤...只是為了讓人們保持警惕。 :)

一個好的方法是將標題更改為:

$to = 'bob@example.com';

$subject = 'Website Change Reqest';

$headers = "From: " . strip_tags($_POST['req-email']) . "\r\n";
$headers .= "Reply-To: ". strip_tags($_POST['req-email']) . "\r\n";
$headers .= "CC: susan@example.com\r\n";
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n";

另外,通過比較來仔細檢查代碼的其余部分:

http://css-tricks.com/sending-nice-html-email-with-php/

我建議不要使用PHP的mail()函數。 是的,它可以發送電子郵件,但對於任何具有任何復雜性的事情,它只是讓事情變得困難。

我建議使用像phpMailer這樣的類,它會給你更多的靈活性。 它可以更輕松地執行諸如發送HTML電子郵件,添加附件和使用其他MTA之類的操作。

我自己有這個問題,通過使用\\ n而不是\\ r \\ n來修復它。 我只使用\\ r \\ n作為標題中的最后一行。

問題是某些防病毒電子郵件掃描程序會將mime標頭后面的任何內容視為正文。

修復是讓你mime標頭最后一個。

我和我的一個客戶遇到了同樣的問題,並且讓我感到震驚了一段時間。

這是Microsoft Outlook(Windows)的問題。

解決方案是刪除標題末尾的“\\ r”,然后使用“\\ n”。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM