Forum Moderators: coopster

Message Too Old, No Replies

mail() and CRLF vs LF

which is right?

         

ergophobe

2:37 pm on Jul 10, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



In another thread [webmasterworld.com], DKLynn posted some code for the mail() function using LF terminators (\n). I was going to post a correction, but decided to check my facts first and what I found was darn confusing. So much so I decided to start another thread!

According to RFC 822: Standard for ARPA Internet Text Messages [w3.org] header lines must be separated by CRLF (\r\n) rather than just LF (\n). See especially section 3.1

This is true according to RFC 821: Simple Mail Transfer Protocol [faqs.org] section 4.1.1

The php documentation for mail() [us3.php.net] supports this, saying: "You must use \r\n to separate headers, although some Unix mail transfer agents may work with just a single newline (\n)." This point is reiterated in comments in the documentaiton from 23 Feb 2003 and 16 Jan 2003.

All good so far, but unfortunately, it appears that it isn't that simple. You actually have to decide between line separators based on the OS that is running PHP and the mailer being used.

For example, in the comments to the documentation for mail() [us3.php.net] Paul (25 Feb 2004) says that CRLF does not work with qmail. See also the comments from 15 April 2003.

It turns out that PHP on *nix uses the native mail function, so in other words, the php command is NOT an SMTP message, but a Unix command line message. Therefore, it misinteprets \r\n and expands \n to \r\n when it sends the message on via SMTP, resulting in \r\r\n This results in extra characters and, apparently, some anti-spam agents use the extra \r as a spam filter so messages created by php may get blocked.

There's a long discussion of this under php bug 15841 [bugs.php.net]

The people who argue in favor of CRLF are basing this on the RFC 821 and 822 protocols. Those against, seem to be basing it on hard experience.

Any experiences in this regard anyone?

Tom

coopster

4:44 pm on Jul 10, 2004 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



It seems to me that it is going to come down to a definition of CRLF by the operating system. The definition of CRLF by the RFC is...


<CRLF> 
The characters carriage return and line feed (in that order).

From some jargon files on the net...


CRLF

A carriage return (CR, ASCII 13) followed by a line feed (LF, ASCII 10). More loosely, whatever it takes to get you from the end of one line of text to the beginning of the next line. Under Unix influence this usage has become less common because Unix uses just line feed as its line terminator (Unix uses a bare line feed as its 'CRLF').

ergophobe

5:41 pm on Jul 10, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Well it does and it doesn't and that's the point.

According to RFC 822 and RFC 821 and the PHP documentation, this should be OS independent and should require an ASCII CRLF.

The question is whether something along the pipeline does a conversion before it gets put into SMTP (or whatever protocol) format. The native unix utils like mail assume that it is getting an LF newline and so converts to CRLF to meet the SMTP protocol. But sendmail should expect and use CRLF. But qmail expects LF.

So if your mail is in SMTP format, but the sending util that lies between PHP and the final send in SMTP format expect some other format, it might get messed up and it might not. From the stuff I read, it seems that some Unix mail utils expect unix newlines and some expect SMTP newlines. On Windows, this should always be CRLF.

At least that's what I get from the threads referenced.

Tom

jatar_k

6:30 pm on Jul 10, 2004 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



I mostly work on FreeBSD and Sun with sendmail for sending with PHP. We use only the LF and everything is fine. I have had to use \r\n when working with Win servers.

Mail from start to finish is a royal pain and it remains my least favourite thing to deal with. I mostly go with \n and if it proves a problem I will swap them to \r\n for a quick run to see if that alleviates my pain.