【问题标题】:PHP - Form mail inserting ! and line break into long stringsPHP-Form 邮件插入!并换行成长串
【发布时间】:2012-01-09 21:11:40
【问题描述】:

我在公司网站上有一个表格,其中包含姓名、电话号码和 cmets(以及其他一些内容)。 cmets 框允许您输入最多 5000 个字符 - 对于非常冗长的客户来说,这是一个很大的限制。有效表单的内容使用 php 表单邮件作为纯文本电子邮件发送到我们的销售部门。

由于某种原因,如果评论超过 1000 个字符,它们会带有感叹号、换行符,有时还会插入缩进。请注意,这仅适用于电子邮件;如果表单中有错误,则将数据插入表单并标记错误,并且 cmets 还没有感叹号+换行符。

我发现一篇关于它的论坛帖子表明存在大约 990 个字符的字符限制会导致此问题。

有人知道原因吗?有谁知道一个相当简单的解决方法?

相关PHP代码:

$to = $email;

$subject = "Website Order Received: $offer";

$contents = "
Order Form Received -\n
Name: $name\n
Company: $company\n
Email: $email\n
Phone: $phone $phoneExt\n
Order Contents:\n" .
($offer == 'web-demo' ? "- I want a live software demonstration.\n" : "") .
($offer == 'pricing' ? "- I'd like pricing information.\n" : "") .
($offer == 'holiday-pricing' ? "- I'd like to sign up before December 31st for the special holiday offer!\n" : "") .
($offer == 'bid-help' ? "- Please give me marketing materials and other assistance for winning bids.\n" : "") .
($offer == 'demo-cd' ? "- Send me the full-version demonstration CD.\n" : "");
if (!empty ($comments)) {
    $comments = str_replace("
", "\n", $comments); // Preserves line breaks in the comments.
    $contents = $contents."\nComments: $comments\n\n";
}
$contents = str_replace("\n", "\r\n", $contents);

mail($to, $subject, $contents);

【问题讨论】:

  • 您有发送邮件供我们检查的 PHP 脚本吗?

标签: php email mail-form


【解决方案1】:

邮件的一行字符数有限制:

此标准对一行中的字符数有两个限制。每行字符必须不超过 998 个字符,并且应该不超过 78 个字符,不包括 CRLF。 (RFC 2882)

您可以使用PHP函数wordwrap来实现:

$contents = wordwrap($contents);

无论如何,这将提高随脚本发送的电子邮件的可读性,并使其符合标准。

【讨论】:

  • 为什么答案不仅仅是 "$contents = wordwrap ..." 这行?为什么之后代码会出现两次“str_replace”?
  • @MarkGoldfain 这是一个非常好的问题,六年后我不知道答案。已编辑。
猜你喜欢
  • 1970-01-01
  • 2011-01-22
  • 1970-01-01
  • 2011-03-05
  • 1970-01-01
  • 2014-05-11
  • 1970-01-01
  • 2021-12-22
相关资源
最近更新 更多