2011-04-14 44 views
0

我可以發送文本很好,只是HTML罰款,但是當我結合兩者如下電子郵件發送但身體是空的。電子郵件正文是空的,MIME多部分(文本和HTML)

$to = "[email protected]"; 
$subject = "Welcome"; 
$boundary = md5(date('U')); 

$headers = "From: Company <[email protected]>" . "\n". 
        "X-Mailer: PHP/".phpversion() ."\n". 
        "MIME-Version: 1.0" . "\n". 
        "Content-Type: multipart/alternative; boundary=".$boundary. "\n"; 
        "Content-Transfer-Encoding: 7bit". "\n"; 

// TEXT EMAIL PART 
$text = "Congratulations"; 

// HTML EMAIL PART 
$html = "<html><body>\n"; 
$html .= "<div>Congratulations</div>"; 
$html .= "</body></html>\n"; 

$message = "Multipart Message coming up" . "\n\n". 
      "--".$boundary. 
      "Content-Type: text/plain; charset=\"iso-8859-1\"" . 
      "Content-Transfer-Encoding: 7bit". 
      $text. 
      "--".$boundary. 
      "Content-Type: text/html; charset=\"iso-8859-1\"". 
      "Content-Transfer-Encoding: 7bit". 
      $html. 
      "--".$boundary."--"; 

mail($to, $subject, $message, $headers);  
+0

我假設你已經設置了$邊界的東西? – bumperbox 2011-04-14 20:49:20

+0

也看看http://swiftmailer.org/可以爲你做很多努力 – bumperbox 2011-04-14 20:50:05

+0

是的,它現在。錯過了當我剪切和粘貼時添加它。 – John 2011-04-14 20:51:14

回答

1

您已在Content-Type和之後忘記換行符內容傳輸編碼線,使你的身體內容的開端共同混合與頭:

 "--".$boundary. 
        ^^^--- missing \n 
     "Content-Transfer-Encoding: 7bit". 
             ^^^--- missing \n\n 
     $text. 

正如評論指出以上,使用SwiftmailerPHPMailer來做到這一點。他們會照顧所有的小細節,讓你用更少的代碼來發送整個郵件。

+0

不能相信這件事很簡單。我會確保看看swiftmailer – John 2011-04-14 20:58:15

+0

爲了生成這樣的多行文本,請查看HEREDOC。比重複字符串連接要容易得多。 http://php.net/heredoc – 2011-04-14 21:01:56

0

$boundary是不確定的,你沒有之前或之後的任何邊界的有斷行,而不必每個部分的標題和正文之間的空行(或任何這些標題後的換行符)。

請勿手動處理MIME。我確信有一個適合PHP的體面的庫,可以爲你做到。

+0

邊界的第三行設置:md5(date('U')); – 2011-04-14 20:53:54

+0

這不是我寫答案的時候。請參閱有關問題的評論。 – Quentin 2011-04-14 20:56:27

+0

啊。問題沒有顯示任何「最後編輯」類型的控件。 – 2011-04-14 20:57:22

相關問題