【问题标题】:Email adding newlines?电子邮件添加换行符?
【发布时间】:2013-11-14 12:02:24
【问题描述】:

我正在尝试使用 php 的 mail() 函数发送 HMTL 电子邮件,并注意到电子邮件中的某些内容已损坏。我在 gmail 中阅读原文发现,邮件正文中的行都是相同的长度,这会在不方便的地方削减 HTML 标签。

我的标题是多部分的,在 html 和纯文本之间有边界,有 7 位。我发送的一个例子是:

<a href="site.com/unsub">Unsubscribe</a>

但是由于它的位置,消息像分裂它一样

<a href="site.co
m/ubsub">unsubscribe</a>

这会破坏超链接。有什么我错过或没有得到的吗?我已经看到过使用像 swiftmailer 这样的邮件库——我可以使用它——但我想真正理解这种行为。

编辑:

mail() 的调用方式:

$headers = 'From: Me <me@site.com>' . "\r\n" . 'MIME-Version: 1.0'
$headers .= "\r\n" . "Content-Type: multipart/mixed; charset=ISO-8859-1; boundary=$boundary"
$subject = 'subject';
$html_message = '--' . $boundary . "\r\n";
$html_message .= 'Content-Type: text/html; charset=ISO-8859-1' . "\r\n";
$html_message .= 'Content-Transfer-Encoding: 7bit' . "\r\n" . "\r\n";
$html_message .= $message . "\r\n";
// plaintext added similarly, with different content-type. Omitted.
mail($to,$subject,$html_message, $headers);

编辑#2:

扩展至$message

$message = "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\"><html xmlns=\"http://www.w3.org/1999/xhtml\"><head><meta http-equiv=\"Content-Type\" content=\"text/html; charset=ISO-8859-1\" /><title>"
$message .= "Page Title</title></head><body style=\"margin: 0; padding: 0; font-family: open \'Open Sans\', \'Open Sans Bold\';\"><table border=\"0\" cellpadding=\"0\" cellspacing=\"0\" width=\"100%\" ><tr><td><table align=\"center\"  bgcolor=\"#FFFFFF\" border=\"0\"cellpadding=\"0\" cellspacing=\"0\" width=\"600\"><tr><td align=\"right\" style=\"padding: 20px 20px 20px 20px;\"> <font color=\"#EA5D0F\">[</font> <a href=\"http://SITE.com\" style=\"color:#000001; text-decoration: none !important;\">Sign In </a><font color=\"#EA5D0F\">][</font> <a href=\"http://about.SITE.com\" style=\"color:#000001; text-decoration: none !important;\">About </a><font color=\"#EA5D0F\">][</font> <a href=\"http:/SITE.com/search\" style=\"color:#000001; text-decoration: none !important;\">Search</a><font color=\"#EA5D0F\">]</font></td></tr><tr><td bgcolor=\"#FFFFFF\" align=\"center\" style=\"padding: 40px 0px 0px 0px;\"><img src=\"http://about.SITE.com/wp-content/uploads/2013/08/SITE.plain_.png\" alt=\"SITE Logo\" width=\"300\" style=\"display: block;\" /></td></tr><tr><td style=\"padding: 10px 20px 20px 20px;\"><table border=\"0\" cellpadding=\"0\" cellspacing=\"0\" width=\"100%\" ><tr><td bgcolor=\"#FFFFFF\" style=\"padding: 30px 10px 10px 10px; text-align:center; font-size: 1.2em; font-weight:bold;\"> <hr color=\"#EA5D0F\"/>"

这种情况持续了很长一段时间。不知道要包括多少。

【问题讨论】:

  • 你是否对这些邮件使用了一些模板系统(如 Twig)?
  • 您确定没有在代码中保存额外的换行符(例如,来自某种文本换行)吗?
  • 您能告诉我们您是如何拨打mail() 的吗?
  • 没有模板系统。没有换行符,看来这个系统的作者没有实现任何类型的换行符。这都是一大块标签,就像缩小的 HTML。
  • 像@AmalMurali 一样称呼他,否则不会通知他。可以添加$message的内容吗?

标签: php html-email


【解决方案1】:

我过去确实遇到过这个问题。虽然我不知道是什么原因,但通过将每个&gt; 替换为&gt;\r\n 解决了这个问题:

$message = str_replace(">", ">\r\n", $message);

【讨论】:

    猜你喜欢
    • 2022-06-29
    • 2015-12-02
    • 2017-11-02
    • 2012-04-02
    • 1970-01-01
    • 1970-01-01
    • 2011-05-01
    • 2021-02-05
    • 1970-01-01
    相关资源
    最近更新 更多