【发布时间】:2016-06-10 14:48:57
【问题描述】:
我到处寻找这个问题,我发现的只是添加以下内容:
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
和
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
我想发送一份时事通讯类型的电子邮件,因此样式对此非常重要。我观看的所有视频都只是制作 html 表格,所以我真的不明白。我想为我的电子邮件中的内容设置样式。
我现在有这个:
$to = $newsletter_email;
$subject = 'Thank you for subscribing';
$message = '
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title></title>
<style>
#email-wrap {
background: #151515;
color: #FFF;
}
</style>
</head>
<body>
<div id="email-wrap">
<p>Hi,</p><br>
<p>Thank you.</p><br>
<p>Thank you,</p>
<p>Administration</p>
</div>
</body>
</html>
';
$from = "newsletter@example.com";
//$Bcc = "example@example.com";
// 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: ' .$to. "\r\n";
$headers .= 'From: ' .$from. "\r\n";
// $headers .= 'Bcc: '.$Bcc. "\r\n";
// Send the email
mail($to,$subject,$message,$headers);
我已经尝试从这个消息变量中取出样式并将这个文件转换成一个 html 样式的文件,在 php 之外:
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Newsletter</title>
<style>
#email-wrap {
background: #151515;
color: #FFF;
}
</style>
</head>
等等
电子邮件实际发送,但我不知道如何为此添加样式。我做错了什么??
$email_from = "newsletter@example.com";
$full_name = 'Company Name';
//$from_mail = $full_name.'<'.$email_from.'>';
$from = $from_mail;
//$from = "newsletter@example.com";
//$Bcc = "example@example.com";
// To send HTML mail, the Content-type header must be set
$headers .= "From: ".$full_name." <".$email_from.">\r\n";
and $headers .= "Return-Path: ".$full_name." <".$email_from.">\r\n";
/*$headers = "" .
"Reply-To:" . $from . "\r\n" .
"X-Mailer: PHP/" . phpversion();*/
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
// Additional headers
$headers .= 'To: ' .$to. "\r\n";
$headers .= 'From: ' .$from_email. "\r\n";
// $headers .= 'Bcc: '.$Bcc. "\r\n";
// Send the email
mail($to,$subject,$message,$headers);
【问题讨论】:
-
电子邮件支持内联css。使用内联css进行邮件设计。
-
请注意,您的代码(以及所有发布的答案)容易受到标头注入攻击,可能还有其他攻击,并且无法正确编码电子邮件内容。不要滚动你自己的电子邮件代码,你会做错的,这一切都证明了;使用像 PHPMailer 这样的库来标记这个问题。
-
您的字符集应该一致——您在 HTML 中设置了 UTF-8,但在邮件标题中设置了 ISO-8859-1;你不能同时拥有两者 - 一旦你有非 ascii 文本,它就会中断
标签: php html css email phpmailer