2009-07-21 343 views
3

大多數電子郵件客戶端在閱讀HTML電子郵件(包括Gmail和Hotmail)中的CSS時遇到問題。我經常使用此服務將我的HTML/CSS轉換爲正確的電子郵件格式,以便在用戶的一端看起來一切正常。基本上它做什麼是將所有的CSS內聯樣式:如何發送HTML/CSS電子郵件?

http://premailer.dialect.ca/

做任何你有你的HTML電子郵件發送CSS任何其他方法?我自動生成電子郵件,由於一些限制,我無法修改內聯樣式。

回答

6

您需要添加一個表示內容爲HTML的標頭。當使用mail()函數時,其中一個標題應該是:內容類型:html/text(可能不是'確切'標題)。

讓我發現你一個例子:(從php.net/mail頁)

// To send HTML mail, the Content-type header must be set 
$headers = 'MIME-Version: 1.0' . "\r\n"; 
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n"; 

// Additional headers 
$headers .= 'To: Mary <[email protected]>, Kelly <[email protected]>' . "\r\n"; 
$headers .= 'From: Birthday Reminder <[email protected]>' . "\r\n"; 
$headers .= 'Cc: [email protected]' . "\r\n"; 
$headers .= 'Bcc: [email protected]' . "\r\n"; 

// Mail it 
mail($to, $subject, $message, $headers); 
+3

不幸的是,發送不帶備用電子郵件的HTML電子郵件被許多垃圾郵件過濾器視爲垃圾郵件。爲了降低觸發機會,您應該將其作爲多部分發送,其中一部分爲text/plain,另一部分爲text/html。 – staticsan 2009-07-21 22:48:24

2

要添加到上面的例子中,(如果你不知道PHP非常好),你只需要建「email」使用變量:to,subject,message和headers

讓我知道如果你想知道如何創建一個表單來填充和運行這個PHP腳本,否則,你可以簡單地在這個文件中輸入所有內容手動保存爲PHP文件,將其放到支持PHP的服務器上,然後導航到瀏覽器中的文件。

下面的代碼:

// Setup recipients 
$to = '[email protected]' . ',' // comma separates multiple addresses 
$to .= '[email protected]'; 

// subject 
$subject = 'PHP Email Script - Test Email'; 

// message (to use single quotes in the 'message' variable, supercede them with a back slash like this-->&nbsp; \' 
$message = ' 
<html> 
<head> 
    <title>PHP Email Script</title> 
</head> 
<body> 
    <p style="background: #ccc; width: 100%;">Test Email Script</p> 
</body> 
</html> 
'; 


// To send HTML mail, the Content-type header must be set 
$headers = 'MIME-Version: 1.0' . "\r\n"; 
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n"; 

// Additional headers 
$headers .= 'To: Mary <[email protected]>, Kelly <[email protected]>' . "\r\n"; 
$headers .= 'From: Birthday Reminder <[email protected]>' . "\r\n"; 
$headers .= 'Cc: [email protected]' . "\r\n"; 
$headers .= 'Bcc: [email protected]' . "\r\n"; 

// Send the email 
mail($to, $subject, $message, $headers); 
+1

爲什麼在$標題中不同於$ to?應該是一樣的吧? – 2011-03-16 19:43:03

3

最簡單的方法是編寫一個HTML頁面嵌入CSS和通過automated styling machine

+0

這是我的解決方案!謝謝 – Luca 2015-05-24 07:52:17

7

至於直接格式推,我一直在做內聯CSS造型,但我使用SwiftMailer(http://swiftmailer.org/)爲PHP5來處理電子郵件功能,它非常有幫助。

您可以發送不同格式的多部分郵件,所以如果電子郵件客戶端不喜歡HTML版本,您可以默認使用文本版本,以便至少知道某些內容正在清理。

在您的「views」文件夾中,您可以爲不同的電子郵件格式設置不同的路由(我也使用smarty,因此擴展名爲.tpl)。下面是一個典型SwiftMailer :: sendTemplate()函數將是什麼,當你設置的模板,如:

$email_templates = array('text/html' => 'email/html/' . $template . '.en.html.tpl', 
         'text/plain' => 'email/text/' . $template . '.en.txt.tpl'); 

foreach ($email_templates as $type => $file) { 
    if ($email->template_exists($file)) { 
    $message->attach(new Swift_Message_Part($email->fetch($file), $type)); 
    } elseif ($type == 'text/plain') { 
    throw new Exception('Could not send email -- no text version was found'); 
    } 
} 

你的想法。 SwiftMailer還有其他一些好東西,包括返回「無法投遞」的地址,記錄傳遞錯誤和限制大量電子郵件批處理。我建議你檢查一下。