【问题标题】:Issue with HTML displayed in email sent from PHP从 PHP 发送的电子邮件中显示 HTML 的问题
【发布时间】:2013-07-25 12:51:31
【问题描述】:

我正在使用 Pear PHP 邮件库来发送 HTML 电子邮件(主要是到 hotmail)。我遇到了 CSS 正常运行的问题。在收到消息的那一刻,CSS 位于消息的正文中,并且不应用样式。

代码如下:

$message = "<!DOCTYPE html>

<html>
<head>
<style>
        body {
    font:12px/1.4em Verdana, sans-serif;
    color:#333;
    background-color:#fff;
    width:700px;
    margin:50px auto;
    padding:0;
}

a {
    color:#326EA1;
    text-decoration:underline;
    padding:0 1px;
}

a:hover {
    background-color:#333;
    color:#fff;
    text-decoration:none;
}

div.header {
    border-bottom:1px solid #999;
}

div.item {
    padding:5px 0;
    border-bottom:1px solid #999;
}  
</style>
</head>
    </body>";

// PARSE    
    $limit = 9;
    for($x=0;$x<$limit;$x++) {
        $title = str_replace(' & ', ' &amp; ', $news[$x]['title']);
        $link = $news[$x]['link'];
        $description = $news[$x]['desc'];
        $date = date('d-m-Y', strtotime($news[$x]['date']));
        $message .= '<p><strong><a href="'.$link.'" title="'.$title.'">'.$title.'</a></strong><br />';
        $message .= '<p>'.$description.'</p>';
    }

    $message .="</body> </html>";       

foreach($email_add_arr as $name => $email)
{

$crlf = "\n";
$from = "";
$to = "$name <$email>";
$subject = "News - $date";
$host = "ssl://smtp.gmail.com";
$port = "465";
$username = "";
$password = "";

// Creating the Mime message
$mime = new Mail_mime();

// Setting the body of the email
$mime->setHTMLBody($message);
$body = $mime->get();   

$headers = array (
'From' => $from,
'To' => $to,
'Subject' => $subject,
'MIME-Version: 1.0',
                    'Content-Type: text/html; charset=ISO-8859-1');
$smtp = Mail::factory('smtp',
                    array ('host' => $host,
                     'port' => $port,
                     'auth' => true,
                     'username' => $username,
                     'password' => $password
                     ));

$mail = $smtp->send($to, $headers, $message);

if (PEAR::isError($mail)) {
    echo("<p>" . $mail->getMessage() . "</p>");
} else {
    echo("<p>Message successfully sent!</p>");
    }
}

有人有这方面的经验吗?

谢谢!

【问题讨论】:

  • 这是您第一次看到内联样式被提倡。电子邮件服务很像互联网浏览器。他们都以不同的方式处理默认值。浏览器比电子邮件服务友好得多。

标签: php html css pear mime


【解决方案1】:

您只能在邮件中应用内联样式。

例如:

<div style="float:left; padding: 10px;">demo</div>

【讨论】:

  • 一个小警告 - 一些邮件客户端将支持样式标签,但为了交叉兼容,您应该使用内联。
  • 是的,非常感谢,但是当发送电子邮件时,电子邮件正文如下所示:

    reddit.com/r/funny/comments/1j0rso/good_point" title="好点。">好点。 a>

    reddit.com/r/funny/comments/1j0rso/good_point/"><img ETC....... hotmail 没有发现它是 HTML 代码。 ..
  • 您的消息已被删除。怎么样?
  • @DanielPilch 这是另一个问题。我回答了你的第一个问题。我建议您为此打开另一个问题。
【解决方案2】:

您只能使用内联 CSS。试试这个工具http://beaker.mailchimp.com/inline-css

【讨论】:

    【解决方案3】:

    @alvaro 的回答是正确的。此外,对于每个客户端所支持的内容,一个很好的资源是 http://www.campaignmonitor.com/css/

    地址

    • 选择器
    • 文字和字体
    • 颜色和背景
    • 盒子模型
    • 列表
    • 表格

    适用于所有相关的邮件客户端

    【讨论】:

      猜你喜欢
      • 2012-12-27
      • 2011-03-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-06-15
      • 2013-10-06
      相关资源
      最近更新 更多