tags:

views:

653

answers:

3

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: [email protected]\r\nReply-To: [email protected]"; 
//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.

+8  A: 

\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: [email protected]" . PHP_EOL 
           . "Reply-To: [email protected]"; 
Yada
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
It shouldn't matter. Just use \n. From my own experience most Windows app accept \n for end of line. $headers = "From: [email protected]".PHP_EOL."Reply-To: [email protected]"; would be the safest code
Yada
This is the wrong answer. Mail headers MUST be separated by \r\n.
Evert
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
+2  A: 

In addition to Yada's answer, here is an explaining blog entry: http://www.codinghorror.com/blog/archives/001319.html

DaDaDom
Yea, I was just reading Jeff's blog couple hours back and I saw this post. Coincident?
o.k.w
+3  A: 

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
my computer was screwing up with /r/n. :(
johnnietheblack