至于直接格式,我总是使用内联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还有许多其他好东西,包括返回"无法传递"的地址,记录传递错误以及限制大型电子邮件批次.我建议你看看.
您需要添加一个标题,说明内容为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)
| 归档时间: |
|
| 查看次数: |
19074 次 |
| 最近记录: |