使用PHP发送跨平台电子邮件,如何处理换行?
当创建使用 PHP mail()
函数发送电子邮件的脚本时,我遇到了新行的问题。 Unix 系统上的 PHP 期望标头用 LF
字符分隔,尽管 文档说,sendmail然后将它们替换为正确的CRLF
。但是在 Windows 上,消息和标头按提供的方式发送。 长期运行的 PHP 错误报告对此进行了描述。
所以我需要一种检测系统是否运行Unix版本的sendmail的方法,以便在Windows上使用LF
和使用CRLF
。我知道 PHP_EOL
但我想知道是否有更优雅的方法来处理这个问题。
目前,我正在按照文档的指定构建我的消息,如下所示。
<?php
$to = "[email protected]";
$subject = "Email Subject Here";
$message = "Hello this is a plaintext\n message with a line break.";
$headers = array(
"From: [email protected]",
"Reply-To: [email protected]",
"X-Mailer: PHP/" . phpversion()
);
$success = mail($to, $subject, $message, join("\r\n", $headers));
if ($success) {
echo "Mail Sent\n";
} else {
echo "Mail Failed\n";
}
导致以下消息被发送到 sendmail(\r
和 \n
已被文本表示替换):
To: [email protected]
Subject: Email Subject HereLF
X-PHP-Originating-Script: 501:mail.phpLF
From: [email protected]
Reply-To: [email protected]
X-Mailer: PHP/5.3.1LF
LF
Hello this is a plaintextLF
message with a line break.LF
在 Unix 系统上,这会 传递到 sendmail 时,所有 LF
都会替换为 CRLF
,从而导致重复的回车符。然后,一些邮件服务器将这个额外的 CR
替换为 CRLF
,从而产生额外的换行符,并且所有标头(在本例中是在 From:
之后)现在都是消息正文的一部分。
PHP 实际上插入了行尾不正确的 – 实际上是 PHP 5.3 bug,现已修复。X-PHP-Originating-Script
标头,这是一个附带问题,但仍然很烦人。
对于处理此跨平台的理想方式有什么想法吗?
谢谢,
阿伦
When creating a script to send emails using the PHP mail()
function I'm coming across issues with new lines. PHP on Unix systems expect headers to be separated with a LF
character, despite what the docs say, sendmail then replaces these with the correct CRLF
. However on Windows the message and headers are sent as provided. This was described in a long running PHP bug report.
So I need a method of detecting whether the system is running the Unix version of sendmail in order to use LF
and use CRLF
on Windows. I'm aware of PHP_EOL
but I'm wondering if there's a more elegant way of handling this.
Currently I'm building my message, as specified by the docs, like so.
<?php
$to = "[email protected]";
$subject = "Email Subject Here";
$message = "Hello this is a plaintext\n message with a line break.";
$headers = array(
"From: [email protected]",
"Reply-To: [email protected]",
"X-Mailer: PHP/" . phpversion()
);
$success = mail($to, $subject, $message, join("\r\n", $headers));
if ($success) {
echo "Mail Sent\n";
} else {
echo "Mail Failed\n";
}
On Unix systems this results in the following message being sent to sendmail (\r
and \n
have been replaced by textual representations):
To: [email protected]
Subject: Email Subject HereLF
X-PHP-Originating-Script: 501:mail.phpLF
From: [email protected]
Reply-To: [email protected]
X-Mailer: PHP/5.3.1LF
LF
Hello this is a plaintextLF
message with a line break.LF
When this is passed to sendmail all LF
are replaced with CRLF
resulting in duplicate carriage returns. Some mail servers then replace this additional CR
with CRLF
resulting in an additional line break and all headers, in this case after From:
, are now part of the message body.
PHP actually inserts the – Actually a PHP 5.3 bug, now fixed.X-PHP-Originating-Script
header with incorrect line ending, which is a side issue but still annoying.
Any ideas on an ideal way of handling this cross platform?
Thanks,
Aron
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这是早期 5.3 版本中的一个相当短暂的缺陷查看此错误通知
升级你的PHP
This was a rather transient defect in the early 5.3 releases see this bug notice
Upgrade your PHP