2012-12-28 102 views
1

我對PHP和消息部分的發送郵件的身體,我的代碼是這樣的:問題在PHP中的電子郵件

$email_body = "<style type=\"text/css\"> 
p {padding:0 !important; margin:0 !important;} 
td {padding:0 10px !important; margin:0 !important; font-family:Verdana, Geneva, sans-serif;} 
td table td {padding:0 !important;} 
</style> 
<table align=\"center\" border=\"0\" cellpadding=\"10\" cellspacing=\"0\" style=\"border-collapse:collapse; border:1px solid #565656; font-family:Verdana, Geneva, sans-serif; width:800px; margin-top:0px; color:#565656;font-weight:normal;font-size:12px; text-align:justify; line-height:1.5em\"> 
    <tbody> 
    <tr> 
    <td style=\"border-collapse:collapse; padding-top:10px !important;\"> 
     message 
    </td> 
    </tr> 
    </tbody> 
</table>"; 

但在我的郵件我得到完整的HTML代碼,無論它是爲$ email_body而不是表格格式編寫的。 如何做到這一點? 謝謝。

+0

也許你需要在你的頭文件中加入這個:''Content-type:text/html; charset = iso-8859-1'。爲 「\ r \ n」 個; ' – gamehelp16

+0

其實我正在發送日曆郵件。如果添加上面的我的標題部分,那麼我沒有得到日曆請求。我已經添加了內容類:urn:content-classes:calendarmessage \ r \ n 和 內容類型:text/calendar;方法= REQUEST;字符集= UTF-8 \ r \ n – phpdeveloper

回答

1

您需要設置正確的頭:

$to  = '[email protected]'; 
$subject = 'foobar'; 
$message = '...'; // the html 
$headers = 'MIME-Version: 1.0' . "\r\n"; 
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n"; 
mail($to, $subject, $message, $headers); 

看一看手冊:http://php.net/manual/en/function.mail.php


你還需要一個完整的HTML文檔,那就是:

html 
    head 
    title 
    style 
    body 
    table 

還有一個提示,如果你發送HTML郵件,不要使用樣式標籤。我經常使用這個premailer:http://premailer.dialect.ca/