php 邮件头中的哪个换行符,\r\n 还是 \n?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4415654/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 12:59:48  来源:igfitidea点击:

Which line break in php mail header, \r\n or \n?

phpemailheader

提问by Sam

I've seen a lot of examples using the php mail function. Some of them use \r\n as line break for the header, some use \n.

我看过很多使用 php 邮件功能的例子。其中一些使用 \r\n 作为标题的换行符,一些使用 \n。

$headers = "From: Just Me\n"; 
$headers .= "Reply-To:  Just me <$email>\n"; 

vs

对比

$headers = "From: Just Me\r\n";
$headers .= "Reply-To:  Just me <$email>\r\n";

which one is correct?

哪一个是正确的?

Sometimes I've had cases where \r\n is used and part of the header is interpreted by some email clients as mail text (losing these header information) - is this because \r\n is wrong?

有时我遇到过使用 \r\n 并且某些电子邮件客户端将部分标题解释为邮件文本(丢失这些标题信息)的情况 - 这是因为 \r\n 是错误的吗?

采纳答案by Russell Dias

The CRLF \r\n, should be used according to the php documentation. Also, to conform to the RFC 2822spec lines must be delimited by the carriage return character, CR \rimmediatelyfollowed by the line feed, LF \n.

CRLF\r\n应根据php 文档使用。此外,要符合RFC 2822规范,行必须由回车符 CR 分隔,\r紧跟其后是换行符 LF \n

Since \r\nis native to Windows platforms and \nto Unix, you can use the PHP_EOL­Docsconstant on Windows, which is the appropriate new line character for the platform the script is currently running on.

由于\r\n是 Windows 平台和\nUnix 的本机,您可以在 Windows 上使用PHP_EOLDocs常量,它是当前运行脚本的平台的适当换行符。

回答by Chuck

Just in case a search engine picks this up and saves someone else the frustration I went through: here's an additional curiousity.

以防万一搜索引擎选择了这一点并为其他人节省了我所经历的挫折:这是一个额外的好奇心。

On php 5.2x on Linux, I had \r\n on my email headers in php mail(), after an upgrade to php 5.3.3, the formatting and sending mysteriously failed. Removing the \r fixed the script (after examining many many other possibilities).

在 Linux 上的 php 5.2x 上,我在 php mail() 中的电子邮件标头上有 \r\n,在升级到 php 5.3.3 后,格式化和发送神秘地失败了。删除 \r 修复了脚本(在检查了许多其他可能性之后)。

回答by Daniel

As stated above, \r\n is what you should use according to the RFC, but this breaks your headers on several mail systems (f.i. Outlook 2003). Even though \n is not the 'proper' line break to use, in my experience it works correctly on all mail systems I've encountered so far. Because of this, I always use just \n.

如上所述, \r\n 是您应该根据 RFC 使用的内容,但这会破坏您在多个邮件系统上的标头 (fi Outlook 2003)。尽管 \n 不是要使用的“正确”换行符,但根据我的经验,它在我迄今为止遇到的所有邮件系统上都能正常工作。因此,我总是只使用 \n。

回答by Scott C

The RFC formally mandates CRLF (\r\n) but using Unix breaks (\n) for headers will save you a lot of hassle. Some mail servers, such as qmail, will reject your message if it uses \r\n.

RFC 正式要求 CRLF (\r\n) 但使用 Unix 中断 (\n) 作为标头将为您节省很多麻烦。如果邮件使用\r\n,某些邮件服务器(例如 qmail)将拒绝您的邮件。

Source: experience, confirmed by this note: http://www.php.net/function.mail#40204

资料来源:经验,经本笔记证实:http: //www.php.net/function.mail#40204

回答by Baker

My experience: HTML emails were working in web clients, but breaking in MS based desktop clients (entourage, outlook). Was using \r\n. Removed the \r on the MIME-Version only and now works across the board.

我的经验:HTML 电子邮件在 Web 客户端中工作,但在基于 MS 的桌面客户端(随行人员、Outlook)中被破坏。正在使用 \r\n。仅删除了 MIME 版本上的 \r,现在可以全面使用。

回答by Kee

I changed my script to use PHP_EOLinstead which seems to work -- like this:

我更改了我的脚本以使用PHP_EOL代替它似乎有效 - 像这样:

//Set Content-type header
$headers  = "MIME-Version: 1.0" . PHP_EOL;
$headers .= "Content-type: text/html; charset=iso-8859-1" . PHP_EOL;
//Additional headers
$headers .= "From: $from" . PHP_EOL;
$headers .= "Cc: $cc"   . PHP_EOL;      
$headers .= "Content-type: text/html" . PHP_EOL;
$headers .= "Bcc: $bcc" . PHP_EOL;

NB. Be sure to us " instead of ' as the latter doesn't seem to work!

注意。一定要我们 " 而不是 ' 因为后者似乎不起作用!

回答by raquo

I've had the problem of gmail misunderstanding \r\n headers, but simply leaving the header line breaks at \n was not enough in my case, because in that case some versions of Outlook showed emails as empty.

我遇到了 gmail 误解 \r\n 标题的问题,但在我的情况下,仅将标题换行符保留在 \n 是不够的,因为在这种情况下,某些版本的 Outlook 将电子邮件显示为空。

The solution in https://stackoverflow.com/a/7960957(I chose to install postfix 2.9 on lucid from a ppa) coupled with using \n seems to work everywhere now.

https://stackoverflow.com/a/7960957 中的解决方案(我选择从 ppa 在 lucid 上安装 postfix 2.9)加上使用 \n 现在似乎在任何地方都可以使用。