簡體   English   中英

使用PHP的郵件發送mime編碼的電子郵件的正確格式

[英]Correct format for sending mime-encoded email using PHP's mail

創建使用PHP的郵件功能發送的mime多部分電子郵件的正確方法是什么?

    $boundary = uniqid(rand(), true);

    $headers =  "From: <noreply@example.com>\r\n";
    $headers .= "Reply-To: <noreply@example.com>\r\n";
    $headers .= "MIME-Version: 1.0\r\n";
    $headers .= "Content-Type: multipart/alternative; boundary=$boundary\r\n\r\n";
    $headers .= "This is a MIME encoded message.\r\n\r\n";
    $headers .= "--$boundary\r\n" .
                "Content-Type: text/plain; charset=ISO-8859-1\r\n" .
                "Content-Transfer-Encoding: base64\r\n\r\n";
    $headers .= chunk_split(base64_encode($plaintext));
    $headers .= "--$boundary\r\n" .
                "Content-Type: text/html; charset=ISO-8859-1\r\n" .
                "Content-Transfer-Encoding: base64\r\n\r\n";
    $headers .= chunk_split(base64_encode($body)); // chunk_split adds the ending "\r\n"
    $headers .= "--$boundary--\r\n";
    //$headers .= ".\r\n";

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

我嘗試了上面的方法,但是PHP似乎忽略了它,而是發送了我在服務器上收到的這封空電子郵件:

...
...
To: <....>
Subject: Test Email
X-PHP-Originating-Script: 0:Mail.php
From: <noreply@example.com>
Reply-To: <noreply@example.com>
MIME-Version: 1.0
Content-Type: multipart/alternative;
    boundary="1923630041519679ed1ddd25.14582240"
Message-ID: <20130517184149.28AB0A4C4@example.com>
Date: Fri, 17 May 2013 14:41:49 -0400
X-UI-Loop: V01:211ZxL2TMQ4=:uQC6SYy+5ULsBgI8/Yn6FAKnX8a66b5mzBQJFWhGo82c
 3a8ZtvbDIKAYJ3vIkfg3

--1923630041519679ed1ddd25.14582240
Content-Type: text/plain; charset="ISO-8859-1"
Content-Transfer-Encoding: base64


--1923630041519679ed1ddd25.14582240
Content-Type: text/html; charset="ISO-8859-1"
Content-Transfer-Encoding: base64


--1923630041519679ed1ddd25.14582240--

在PHP 5.4.9上測試。

我上面的代碼工作。 似乎我需要用linux \\n替換所有Windows \\r\\n \\n 我還刪除了border boundary="..."之后的重復換行符。

暫無
暫無

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

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