2009-12-05 63 views
0

我試圖發送一封電子郵件給我自己,它有一個佈局和圖像。我做錯了什麼?在php中發送html電子郵件的問題

<?php 
$message = $_POST['message']; 
$emailsubject = 'site.com'; 
$webMaster = '[email protected]'; 

$body = " 
<html> 
<body bgcolor=\"e7e7e7\"> 
<style type=\"text/css\"> 
#body {margin: auto;border: 0;padding: 0;font-family: Georgia, 'Times New Roman',  Times, serif;font-size: 12px;} 
#emailHeader {width: 500px;height: 131px;background:  url(http://www.site.com/images/image.gif) no-repeat;} 
#emailContent {width: 500px;background: url(http://www.site.com/images/image2.gif) repeat-y;text-align: left;padding: 0 33px 0 6px;} 
#emailFooter {width: 500px;height: 34px;background:  url(http://www.site.com/images/image3.gif) no-repeat;} 
</style> 
<table width=\"100%\" border=\"0\" cellspacing=\"0\" cellpadding=\"0\"> 
<tr> 
<td valign=\"top\" align=\"center\"> 
<table width=\"500\" border=\"0\" cellspacing=\"0\" cellpadding=\"0\"> 
<tr> 
<td id=\"emailHeader\"></td> 
</tr> 
<tr> 
<td id=\"emailContent\"> 
content $message 
</td> 
</tr> 
<tr> 
<td id=\"emailFooter\"></td> 
</tr> 
</table> 
</td> 
</tr> 
</table> 
</body> 
</html>" 
$headers .= "Content-type: text/html\r\n"; 
$success = mail($webMaster, $emailsubject, $body, $headers); 

if ($success) { 
    echo "Your message was sent."; 
} 
else{ 
    echo "There was a error."; 
} 
?> 
+2

發生什麼事情並非您所期望的?你是否收到錯誤信息?如果是這樣,什麼? – Amber 2009-12-05 00:47:35

+0

@PHPNooblet。你有什麼問題?電子郵件是否顯示不正確?是否根本沒有顯示?請編輯你的問題來解釋。 – 2009-12-05 00:48:02

+0

佈局不顯示。沒有圖片,任何東西。當我在HTML文件中查看佈局時,它工作正常。 – PHPNooblet 2009-12-05 00:50:16

回答

3

您應該使用phpmailer而不是PHP的mail()函數。它可以讓你輕鬆發送HTML郵件。

除此之外,你可以嘗試validate你的HTML代碼兼容電子郵件。

最良好的祝願, 費邊

+0

我會嘗試。謝謝。 – PHPNooblet 2009-12-05 00:55:51

1

你在你的代碼中的錯誤:

WRONG

$headers .= "Content-type: text/html\r\n"; 

RIGHT

$headers = "Content-type: text/html\r\n"; 

.=在PHP中拋出一個解析錯誤,除非您先前在其他位置設置了$headers

它也可能取決於您正在測試的電子郵件客戶端。請務必查看http://www.email-standards.org/以檢查您的電子郵件客戶端支持的內容。

+0

PHP足夠寬容。你不一定需要預定義它。 – BalusC 2009-12-05 01:06:21