28

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?

Sam
  • 28,421
  • 49
  • 167
  • 247

8 Answers8

23

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.

hakre
  • 193,403
  • 52
  • 435
  • 836
Russell Dias
  • 70,980
  • 5
  • 54
  • 71
  • 1
    I wonder why some mail clients interpret \r\n as being two line breaks (thunderbird seems to be among them). – Sam Dec 11 '10 at 07:33
  • @Sam Unfortunately, I'm not entirely sure. Please let me know if you stumble across an answer. – Russell Dias Dec 11 '10 at 07:40
  • I made a question from this problem, maybe someone else knows the answer. – Sam Dec 11 '10 at 07:53
  • 20
    This answer is not really logical: if the php document and the RFCs specifically request the use of `\r\n`, the using the constant `PHP_EOL` will certainly violate that on all non-MS-Windows platforms. Why should one use it? Use the literal `\r\n` and all is fine. – arkascha Apr 08 '15 at 07:25
  • 3
    I notice that when using \r\n in the php $headers argument for the mail function (4th argument), my local MTA (postfix via sendmail) is converting \n to \r\n. The resulting sent email then contains \r\r\n which screws up the headers on some clients. Perhaps the reason to use PHP_EOL is because the MTA is expecting native line delimiters from sendmail. That would then make sense that on a unix system it would need to convert the \n to \r\n to be compatible with the RFC. – Phil Aug 06 '15 at 20:48
13

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

Chuck
  • 177
  • 2
  • 9
9

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.

Daniel
  • 91
  • 1
  • 1
  • 2
    A list of email clients on which \n by itself causes headers to be misinterpreted would be useful. It seems that rare and foolish is the email client programmer who would use the \n character as part of an email header's value, since the RFC disallows this explicitly: "A field body may be composed of any US-ASCII characters, except for CR and LF." (Section 2.2). The behavior of the client when there is a \n by itself before the two newlines that terminates the headers is undefined, so using \n as a header value terminator is more sensible than following the RFC. – Dave Scotese Mar 22 '14 at 00:55
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

Scott C
  • 744
  • 1
  • 6
  • 19
  • 3
    Actually, the reason why everyone is saying that \r\n doesn't work and \n does work is because they are on a unix environment sending mail locally. This is not the same as sending an email directly to a mail server with SMTP. – Phil Aug 06 '15 at 21:50
2

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.

Baker
  • 21
  • 3
1

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.

Community
  • 1
  • 1
raquo
  • 11
  • 1
1

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!

Kee
  • 19
  • 1
0
> $mail = new PHPMailer;
 $mail->isSMTP(); 
**$mail->isHTML(true);**

Insert this code after working

> all html tag <br> <p> in $mail->Body='Hello<br> how are you ?<b>';
Shabari nath k
  • 920
  • 1
  • 10
  • 23