tags:

views:

50

answers:

3

I see a lot of PHP email implementations using "\r\n", but I have also seen some of them using the PHP_EOL constant. Which one is better?

Thanks for any help
Metropolis

A: 

\r\n is the Windows newline, if you're customers are mostly Windows users then this will do you fine.

I also beleive that most non-Windows email clients will compensate for this anyway, so \r\n should not be an issue on Mac or *Nix.

thomasfedb
+1  A: 

Just to make sure:

PHP_EOL will have no effect on the email at the receiver side. It will use the new line character combination that is common on the system your PHP script is running on.

So if you have a Linux/Unix server, PHP_EOL will result in \n and if you have a Windows server, it will be \r\n.

Today, it should not matter that much which you use and I think that most Windows email applications can also handle just \n (afaik even WordPad understands this, it is Notepad that has problems).

Felix Kling
+8  A: 

If this is to terminate lines in an email then it's the spec for email that you need to look at, not what is used on any particular platform.

Lines in email are terminated by CRLF ("\r\n") according to RFC2821

SMTP commands and, unless altered by a service extension, message data, are transmitted in "lines". Lines consist of zero or more data characters terminated by the sequence ASCII character "CR" (hex value 0D) followed immediately by ASCII character "LF" (hex value 0A). This termination sequence is denoted as in this document. Conforming implementations MUST NOT recognize or generate any other character or character sequence as a line terminator

That seems pretty clear that in an email the end of line is to be sent as \r\n . Sending anything else might work but it's wrong unless you are using a "service extension" and if you are then you probably know what you should be sending anyway.

John Burton
+1 for consulting the RFC. Now that you've posted that, I have a vague recollection of reading that once.
George Marian
Awesome. Thanks for the great explanation JB.
Metropolis