4

PHP manual (http://php.net/manual/en/function.mail.php) says:

Each line should be separated with a LF (\n). Lines should not be larger than 70 characters.

But actual RFC 5322 gives totallty different information:

2.3. Body The body of a message is simply lines of US-ASCII characters. The only two limitations on the body are as follows:
o CR and LF MUST only occur together as CRLF; they MUST NOT appear independently in the body. o Lines of characters in the body MUST be limited to 998 characters, and SHOULD be limited to 78 characters, excluding the CRLF.

So - RFC says that only \r\n should be used. I don't understand - how does php mail() work in the background?

Brian Webster
  • 30,033
  • 48
  • 152
  • 225
Stann
  • 13,518
  • 19
  • 65
  • 73
  • 3
    PHP's open source... you can quite easily go read the source and see what mail() is doing. Just because the mail() docs say to provide things one way and the underlying RFC says something else doesn't mean PHP can't be doing some translating internally. – Marc B Jul 25 '11 at 21:58
  • 1
    There are things in this bug that might interest you : https://bugs.php.net/bug.php?id=15841 *(not sure it's an exact answer, but you should find some informations, about qmail, sendmail, windows, and all)* – Pascal MARTIN Jul 25 '11 at 22:03
  • 1
    @Marc B: sure it's easy, why would going through massive c codebase be hard for php developer? I eat problems like that for breakfast. – Stann Jul 25 '11 at 22:57

2 Answers2

2

Obviously PHP mail() must be converting \n to \r\n in the background.

This is similar to Perl's practice of using \n to represent the "logical" newline and then translating them internally depending on the OS. Since PHP was partially inspired by Perl, it's not really a surprise that a similar mechanism exists in PHP.

Platinum Azure
  • 45,269
  • 12
  • 110
  • 134
1

how does php mail() work in the background?

Exactly as you might expect from the cionfiguration. By default its simply a wrapper around the sendmail binary on most systems and a very simple MUA where an SMTP host is specified. While the former uses a LF as a line ending in its input, the latter requires a CRLF for its output - since line endings vary by OS, PHP provides a unified line ending for mail (LF)

symcbean
  • 47,736
  • 6
  • 59
  • 94
  • 2
    I would argue that this is exactly what one wouldn't expect. Rf5321/5322 clearly says - use \r\n for headers and body. and then it clearly says - do not use single \n or single \r. Why PHP chose to use \n and not \r\n right away to avoid double conversion - i do not know. – ThatGuy Jul 25 '11 at 23:06