Page 1 of 1

differ '\r\n' from '\n' in the email header?

Posted: Tue Dec 18, 2007 2:55 pm
by xinper2007
feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]


I found a mail() function example in the php.net, but it doesn't work on my server. However if I changed '\r\n' into '\n' in the $headers, it would work perfectly. What is the difference between '\r\n'and '\n' in the email header? Or is there some settings in the php.ini can impact on this?


http://www.php.net/manual/en/function.mail.php

Code: Select all

<?php
// multiple recipients
$to  = 'aidan@example.com' . ', '; // note the comma
$to .= 'wez@example.com';

// subject
$subject = 'Birthday Reminders for August';

// message
$message = '
<html>
<head>
  <title>Birthday Reminders for August</title>
</head>
<body>
  <p>Here are the birthdays upcoming in August!</p>
  <table>
    <tr>
      <th>Person</th><th>Day</th><th>Month</th><th>Year</th>
    </tr>
    <tr>
      <td>Joe</td><td>3rd</td><td>August</td><td>1970</td>
    </tr>
    <tr>
      <td>Sally</td><td>17th</td><td>August</td><td>1973</td>
    </tr>
  </table>
</body>
</html>
';


/////////////////////////////////////////////////////////////
// to have this code worked, I have to change "\r\n" into "\n"
////////////////////////////////////////////////////////////
// To send HTML mail, the Content-type header must be set
$headers  = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";

// Additional headers
$headers .= 'To: Mary <mary@example.com>, Kelly <kelly@example.com>' . "\r\n";
$headers .= 'From: Birthday Reminder <birthday@example.com>' . "\r\n";
$headers .= 'Cc: birthdayarchive@example.com' . "\r\n";
$headers .= 'Bcc: birthdaycheck@example.com' . "\r\n";

// Mail it
mail($to, $subject, $message, $headers);
?>

feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]

Posted: Tue Dec 18, 2007 3:04 pm
by impulse()
As far as I know it's only to do with operating systems. Windows uses '\r' and I think the majority of others use '\n'. By using '\r\n' you accommodate for the majority of operating systems.

But looking at your problem it seems strange that taking extra measures creates a problem. My best theory is that you're using a Microsoft Exchange server and that Exchange sends all e-mails that have anything to do with any other OS (IE - \n) to it's bin :) - Only joking there, I don't know really.

Regards,

Posted: Tue Dec 18, 2007 3:59 pm
by xinper2007
But my OS is redhat linux, PHP version is 4.3.9, QMail

Posted: Tue Dec 18, 2007 6:34 pm
by feyd
If memory serves, \r\n is the standard.

But seriously, use a mailing library, even for the most basic of emails. We prefer Swift Mailer around here.

Posted: Tue Dec 18, 2007 11:04 pm
by claws
yepp.. Swift Mailer is too gud.
i started using from yesterday only.

btw.. can some one tell me the difference b/w

'\n' , '\r'

or

1. newline
2. formfeed
3. carriage return.

Posted: Wed Dec 19, 2007 7:10 am
by feyd
If memory serves, \n is carriage return, \r is linefeed.

Posted: Wed Dec 19, 2007 3:06 pm
by s.dot
\r\n = DOS line ending
\n = nix line ending
\r = mac line ending

As far as I'm aware.

\r is a carriage return
\n is a line feed
\r\n is both of them

For scripting, PHP has a constant PHP_EOL, that will represent the line ending on the server.

Code: Select all

echo 'hi' . PHP_EOL . 'how are you?';
For mail, I believe \r\n is what's good, or its equivalent 0x0A (or something similar :P)

Some good solid information: http://en.wikipedia.org/wiki/Line_feed