1

In the PHP manual here, it shows header lines ending in "\r\n" but it never explains why.

I have been googling this one, but most results explain why "\r\n" is more compatible than "\n", however this answer must be too advanced for me because I can't understand why headers need either.

I understand it is used to display things on a new line, but why should that matter for email headers?

What does the "\r\n" do (in the context of email headers)? Is there a situation where I may not want the "\r\n"? What do I risk by leaving out the "\r\n"? Is this a concept exclusive to the mail() function or is there an entire category of functions that requires "\r\n"?

  • 4
    [Because that's the standard.](https://www.ietf.org/rfc/rfc2822.txt) – John Conde Aug 29 '18 at 01:18
  • So is using four spaces instead of tab. I like to understand standards before I break them. – Jonathon Philip Chambers Aug 29 '18 at 01:20
  • 2
    hardly comparable, for one the code would still work, for the other the mail would not be delivered. –  Aug 29 '18 at 01:23
  • 1
    No, literally that _is_ the standard. Tabs vs spaces is a convention at best. – msbit Aug 29 '18 at 01:23
  • 3
    CRLF is used for [most network protocols](https://stackoverflow.com/questions/5757290/http-header-line-break-style). Though for mails specifically, depends on if your sending raw messages, or have an MTA which does implicit cleanup. → Better safe than sorry? – mario Aug 29 '18 at 01:26
  • 1
    @mario if you wrote that as an answer, that would get my upvote. If it was still the most detailed answer by this time tomorrow, I'd mark it as correct also. – Jonathon Philip Chambers Aug 29 '18 at 01:29
  • 1
    i'm not sure any one understands what answer you expect, other than, "its the rules" –  Aug 29 '18 at 01:31
  • @IdontDownVote I thought mario's answer was definitely along the right lines. Your mention of the code not working if it wasn't included would also be the correct answer if it was the only answer given. – Jonathon Philip Chambers Aug 29 '18 at 01:40

1 Answers1

1

I understand it is used to display things on a new line, but why should that matter for email headers?

Its probably just the parsing strategy for the headers. After a CRLF, check for the next header...

What does the "\r\n" do (in the context of email headers)?

As i said, parsing strategy..

Is there a situation where I may not want the "\r\n"?

In PHP 7.2.0 you can pass an array instead of a string... but its probably just doing the processing for you.

What do I risk by leaving out the "\r\n"?

A non delivered email.

Is this a concept exclusive to the mail() function or is there an entire category of functions that requires "\r\n"?

Not that i remember... but in this case, i think it would be for parsing too.

Erubiel
  • 2,934
  • 14
  • 32