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

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
xinper2007
Forum Newbie
Posts: 5
Joined: Mon Apr 16, 2007 4:56 pm

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

Post 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]
impulse()
Forum Regular
Posts: 748
Joined: Wed Aug 09, 2006 8:36 am
Location: Staffordshire, UK
Contact:

Post 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,
xinper2007
Forum Newbie
Posts: 5
Joined: Mon Apr 16, 2007 4:56 pm

Post by xinper2007 »

But my OS is redhat linux, PHP version is 4.3.9, QMail
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post 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.
claws
Forum Commoner
Posts: 73
Joined: Tue Jun 19, 2007 10:54 am

Post 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.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

If memory serves, \n is carriage return, \r is linefeed.
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Post 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
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
Post Reply