20

Simple question...

I have seen people tell me to use "\r\n" in various places and others tell me to use "\n" in the same place. I'm sure one is right and one is wrong. Example - when designing mail() headers:

Tutorial #1:

//define the headers we want passed. Note that they are separated with \r\n 
$headers = "From: webmaster@example.com\r\nReply-To: webmaster@example.com"; 
//add boundary string and mime type specification 
$headers .= "\r\nContent-Type: multipart/mixed; boundary=\"PHP-mixed-".$random_hash."\""; 

Tutorial #2 (notice the header argument):

mail($to, $subject, $body, 
    "From: " . $from . "\n" . 
    "bcc: " . $bcc . "\n" . 
    "MIME-Version: 1.0\n" . 
    "Content-Type: multipart/alternative;\n" . 
    "     boundary=" . $mime_boundary_header)

I am confused, but clearly it makes somewhat of a difference, because with one, my headers worked, and with the other they only sometimes work.

johnnietheblack
  • 13,050
  • 28
  • 95
  • 133
  • The problem here is both can be potentilly correct. It alldepneds on what you are doing with the strings. Tut 1: You are just creating strings (not doing anything with them). Tut 2: You are passing strings to a function "mail()". If in Tut 1: you stick these directly on a socket it may work (as it seems to conform to RFC 821) but you are down and dirty in the implementation details. In Tut 2: it depends on what the documentation for mail() says is the correct way to send string. Internally it may be converting the text for you. – Martin York Mar 24 '11 at 19:01

3 Answers3

27

\r\n are end of line characters for Windows systems.

\n is the end of line character for UNIX systems.

These characters are invisible. From my own experience \n is usually okay for Windows as well.

Some prefer to use PHP_EOL constant instead of these characters for portability between platforms.

echo 'hi' . PHP_EOL;
echo "hi\n";

$headers = "From: webmaster@example.com" . PHP_EOL 
           . "Reply-To: webmaster@example.com"; 
Community
  • 1
  • 1
Yada
  • 30,349
  • 24
  • 103
  • 144
  • so it doesnt matter on the client then? example...when formulating the headers for my mail(). if i am running a linux, i should use \n instead of \r\n right? OR do i need to worry about what the reader's system is as well? – johnnietheblack Jan 22 '10 at 06:53
  • It shouldn't matter. Just use \n. From my own experience most Windows app accept \n for end of line. $headers = "From: webmaster@example.com".PHP_EOL."Reply-To: webmaster@example.com"; would be the safest code – Yada Jan 22 '10 at 06:57
  • 7
    This is the wrong answer. Mail headers MUST be separated by \r\n. – Evert Jan 22 '10 at 08:38
  • 1
    evert...the question wasn't about the mail headers...that was just an example. i wanted to know about what \r and \n is. BUT...after switching my mail headers to \n it seemed to work even better. can you direct me in the direction of a great tutorial for that? – johnnietheblack Jan 22 '10 at 17:29
  • 1
    This answer is correct but not the correct answer to the question. This answer deals with platform specific EOL sequence for files stored in the file system. The question is about the characters needed for using with a protocol. In this case the mail protocol RFC 821. Which clearly indicates that you should be using "" => "\r\n"; – Martin York Mar 24 '11 at 18:58
6

As per RFC 821 (the SMTP protocol), line endings should always be \r\n (<CR><LF>) in mail headers and content, but in practice it shouldn't matter as most mail servers handle all three type of line endings correctly (supposedly, some old UNIX-based ones actually choke on \r\n but not \n).

Max Shawabkeh
  • 37,799
  • 10
  • 82
  • 91
5

In addition to Yada's answer, here is an explaining blog entry: https://blog.codinghorror.com/the-great-newline-schism/

[Update 2017-05-24: Fixed the link]

Dominik Sandjaja
  • 6,326
  • 6
  • 52
  • 77