从PHP发送的电子邮件中显示的HTML问题


Issue with HTML displayed in email sent from PHP

我正在使用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>");
    }
}

有人有这方面的经验吗?

谢谢!

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

例如:

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

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

@alvaro的答案是正确的。此外,对于每个客户端到底支持什么,一个很好的资源是http://www.campaignmonitor.com/css/

它针对

  • 选择器
  • 文本&字体
  • color&背景
  • 箱形模型
  • 列表

对于所有相关的邮件客户端