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

2019-01-07 16:39发布

问题:

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.

$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?

回答1:

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

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



回答2:

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).



回答3:

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.



回答4:

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.

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



回答5:

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.



回答6:

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.

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.



回答7:

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

//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!



标签: php email header