如何发送HTML/CSS电子邮件?

Jam*_*ore 4 css php email xhtml

大多数电子邮件客户端在阅读HTML电子邮件(包括Gmail和Hotmail)中的CSS时遇到问题.我经常使用这项服务将我的HTML/CSS转换为正确的电子邮件格式,以便在用户端看起来一切正常.基本上它的作用是将所有CSS转换为内联样式:

http://premailer.dialect.ca/

你们有没有其他方法可以在HTML电子邮件中发送CSS?我自动生成电子邮件,由于一些限制,我无法修改内联样式.

ajh*_*406 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');
  }
}
Run Code Online (Sandbox Code Playgroud)

你明白了.SwiftMailer还有许多其他好东西,包括返回"无法传递"的地址,记录传递错误以及限制大型电子邮件批次.我建议你看看.


Tyl*_*ter 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 <mary@example.com>, Kelly <kelly@example.com>' . "\r\n";
$headers .= 'From: Birthday Reminder <birthday@example.com>' . "\r\n";
$headers .= 'Cc: birthdayarchive@example.com' . "\r\n";
$headers .= 'Bcc: birthdaycheck@example.com' . "\r\n";

// Mail it
mail($to, $subject, $message, $headers);
Run Code Online (Sandbox Code Playgroud)

  • 不幸的是,发送没有备用的HTML电子邮件被许多垃圾邮件过滤器视为垃圾邮件.为了降低触发它的机会,你应该将它作为多部分发送,一个部分为text/plain,另一个为text/html. (3认同)