2013-05-17 127 views
0

使用PHP郵件功能創建MIME多部分電子郵件的正確方法是什麼?使用PHP郵件發送MIME編碼電子郵件的正確格式

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

    $headers = "From: <[email protected]>\r\n"; 
    $headers .= "Reply-To: <[email protected]>\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: <[email protected]> 
Reply-To: <[email protected]> 
MIME-Version: 1.0 
Content-Type: multipart/alternative; 
    boundary="1923630041519679ed1ddd25.14582240" 
Message-ID: <[email protected]> 
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。

回答

0

我得到了上面的代碼工作。似乎我需要用linux \n替換所有的窗口\r\n。我也在boundary="..."之後刪除了重複的換行符。