PDA

View Full Version : Double line breaks in mail body


ro1960
06-26-2006, 06:57 PM
When using mail(), the body of the messages end up having double number of line breaks.

For example, this text typed in the form:

Hello!

Can you send me your catalog?

Thank you!
Mr. Smith.


ends up like this in my mail program:

Hello!



Can you send me your catalog?



Thank you!

Mr. Smith.


I tried to add this to my mail headers without success:

// Additional headers
$headers* = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/plain; charset=iso-8859-1' . "\r\n";

lavinpj1
06-26-2006, 07:00 PM
It won't be the headers, it'd be in the message. You'd have something like...

$message = "Hello!\n\r\n\rCan you send me your catalog?\n\r\n\rThank you!\n\rMr. Smith.";

~Phil~

TheShaner
06-26-2006, 08:07 PM
If my understanding is correct, if you do something like:
$message = "Hello.\r\n\r\nBye";
You will end up with four line breaks if your email program does not interpret the \r\n (Carriage Return Line Feed) as a single new line. All mail programs interpret the \n as a new line without having a \r in front. So I would stick to just doing this:
$message = "Hello.\n\nBye";
This will give you your one line break in your email. If you were to check out the mail function (http://us3.php.net/manual/en/function.mail.php), you'd see that they suggest only using \n in the message.

-Shane

arnyinc
06-26-2006, 09:19 PM
I ran into an issue where I was specifying line breaks in my email messages and I was also calling the nl2br() function around my $message variable, which effectively duplicated line breaks on those occassions. This would be more applicable to html formatted emails, but are you wrapping any functions around the message you send?

ro1960
06-27-2006, 12:42 AM
After an extended research, I found the cure to my problem. It's str_replace

$message .= str_replace("\r", "", $from_message);

More about this at:
http://fr2.php.net/manual/en/function.str-replace.php

lavinpj1
06-27-2006, 07:03 AM
We all know what str_replace is. All you are doing there, is removing carrage returns (\r) from $from_message and putting it on the end of $message. I don't see how that gives you double line breaks :S

~Phil~

ro1960
06-30-2006, 11:02 AM
We all know what str_replace is. All you are doing there, is removing carriage returns (\r) from $from_message and putting it on the end of $message. I don't see how that gives you double line breaks :S

~Phil~

This is the solution to my own problem Phil.
What I'm doing is removing unwanted "\r". I replace them with nothing. Then I no longer have the double line returns in the email. This seems to work OK.

d11wtq
06-30-2006, 11:13 AM
If my understanding is correct, if you do something like:
$message = "Hello.\r\n\r\nBye";
You will end up with four line breaks if your email program does not interpret the \r\n (Carriage Return Line Feed) as a single new line. All mail programs interpret the \n as a new line without having a \r in front. So I would stick to just doing this:
$message = "Hello.\n\nBye";
This will give you your one line break in your email. If you were to check out the mail function (http://us3.php.net/manual/en/function.mail.php), you'd see that they suggest only using \n in the message.

-Shane

I'd strongly suggest reading the RFC (2821). It's loosely referred to but you should only use \r\n in emails as line endings. Lone LF or CR is technically not allowed. I'm not just being pedantic, I have reasons for saying this. qMail explicitly REFUSES to send emails with \n or \r not as a \r\n pair.

You can see the message it gives you here:

http://pobox.com/~djb/docs/smtplf.html

TheShaner
06-30-2006, 02:37 PM
I'd strongly suggest reading the RFC (2821). It's loosely referred to but you should only use \r\n in emails as line endings. Lone LF or CR is technically not allowed. I'm not just being pedantic, I have reasons for saying this. qMail explicitly REFUSES to send emails with \n or \r not as a \r\n pair.

You can see the message it gives you here:

http://pobox.com/~djb/docs/smtplf.html
Interesting. Definitely learned something new, thanks! So you'll have to deal with double line breaks with \r\n on some mail servers and single line breaks with others? The message there doesn't state anything about bare carriage returns, although it does say that line endings should be CRLFs. Would using bare CRs be a problem with some mail servers? Basically, is there a universal way of displaying a single and double line break?

-Shane

felgall
06-30-2006, 10:05 PM
In practice \n works fine by itself for both sendmail and qmail for the body content of the email. For the headers sendmail requires \r\n while qmail requires \n. I haven't tested any of the other mail processors so I can't comment on those but no one using my form2mail script has ever hit a problem with it so either they are all using sendmail or qmail or the one they are using works the same as one of those two.