【问题标题】:How to send HTML/CSS emails?如何发送 HTML/CSS 电子邮件?
【发布时间】:2010-11-12 16:32:16
【问题描述】:

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

http://premailer.dialect.ca/

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

【问题讨论】:

    标签: php css email xhtml


    【解决方案1】:

    至于直接格式,我一直使用内联 CSS 样式,但是我使用 PHP5 的 SwiftMailer (http://swiftmailer.org/) 来处理电子邮件功能,它帮助很大。

    您可以发送不同格式的多部分消息,因此,如果电子邮件客户端不喜欢 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 还有很多其他的好东西,包括返回“无法投递”的地址、记录投递错误和限制大量电子邮件。我建议你检查一下。

    【讨论】:

      【解决方案2】:

      最简单的方法是写一个嵌入css的html页面,通过automated styling machine推送

      【讨论】:

      • 这就是我的解决方案!谢谢
      【解决方案3】:

      要添加到上面的示例,(如果您不太了解 PHP),您只需使用以下变量构建“电子邮件”:to、subject、message 和 headers

      如果你想知道如何创建一个表单来填写和运行这个 PHP 脚本,请告诉我,否则,你可以简单地手动输入这个文件中的所有内容,保存为一个 PHP 文件,把它扔到支持的服务器上PHP,然后在浏览器中导航到该文件。

      代码如下:

      // Setup recipients
      $to = 'johndoe@google.com' . ',' // comma separates multiple addresses
      $to .= 'janedoe@msn.com';
      
      // subject
      $subject = 'PHP Email Script - Test Email';
      
      // message (to use single quotes in the 'message' variable, supercede them with a back slash like this-->  \'
      $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 <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";
      
      // Send the email
      mail($to, $subject, $message, $headers);
      

      【讨论】:

      • 为什么 $headers 中的 to 与 $to 中的不同?应该是一样的吧?
      【解决方案4】:

      您需要添加一个标头,说明内容是 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);
      

      【讨论】:

      • 不幸的是,发送没有备用的 HTML 电子邮件被许多垃圾邮件过滤器视为垃圾邮件。为了降低触发它的机会,您应该将其作为多部分发送,其中一个部分为 text/plain,另一部分为 text/html。
      猜你喜欢
      • 2011-01-01
      • 2014-09-23
      • 1970-01-01
      • 2012-10-31
      • 2011-03-30
      • 2012-11-18
      • 1970-01-01
      相关资源
      最近更新 更多