Jump to content

Email \r\n sometimes not working


Wickham

Recommended Posts

I have an online form which sends two emails, one to the user and one to the admin.

mail( $email, "Village Hall booking", "$club\r\n$room $book\r\n$day/$month/$year $starttime to $endtime\r\n$replymsg", "From: $admin" );

mail( $admin, "Village Hall booking", "$club\r\n$room $book\r\n$day/$month/$year $starttime to $endtime\r\n$who $phone $email\r\n$message", "From: $admin" );

The \r\n works in all cases except one which is the last in the second email between $email and $message but it does work between all the others including the $endtime and $replymessage in the first email. So in an email received by admin the last line is text from $who $phone $email $message all on one line.

 

The only difference is that $email is an input type="text" whereas the $message is a textarea but I don't see why that should make a difference.

 

The variables don't extract from a database, they are straight from the form post after remove_headers and stripslashes.

 

Any ideas why this is?

 

What's interesting is that I've just changed the order to

mail( $email, "Village Hall booking", "$day/$month/$year $starttime to $endtime\r\n$room $book\r\n$club\r\n$replymsg", "From: $admin" );

mail( $admin, "Village Hall booking", "$day/$month/$year $starttime to $endtime\r\n$room $book\r\n$club\r\n$who $phone $message\r\n$email", "From: $admin" ); 

 

for the second email and the first three

\r\n

work to give a new line but there is no line break between $message and $email so $who $phone $message and $email are all on the same line, so it's definitely the last \r\n that doesn't want to work whatever variable comes after it.

 

It's very odd. I decided to wrap up all the body part of the email in one variable $body for the second email

$body=$day . "/" . $month . "/" . $year . ", " . $starttime . " to " . $endtime . "\r\n" . $room . ", " . $book . "\r\n" . $club . "\r\n" . $who . ", " . 
$phone . ", " . $email . "\r\n" . $message;

mail( $email, "Village Hall booking", "$day/$month/$year $starttime to $endtime\r\n$room $book\r\n$club\r\n$replymsg", "From: $admin" );

mail( $admin, "Village Hall booking", $body, "From: $admin" ); 

and as before all the \r\n worked except the very last one between $email and $message (a few commas were added but that's irrelevant).

Edited by Wickham
Link to comment
Share on other sites

Have you tried just the Line Feed(\n) without the Carriage Return(\r). What server are you running it through? If it's Unix, this may be the problem from what I've read. Just a shot in the dark. I'm really green when it comes to php. good luck!

 

mail( $email, "Village Hall booking", "$club\n$room $book\n$day/$month/$year $starttime to $endtime\n$replymsg", "From: $admin" );

 

mail( $admin, "Village Hall booking", "$club\n$room $book\n$day/$month/$year $starttime to $endtime\n$who $phone $email\n$message", "From: $admin" );

Link to comment
Share on other sites

The \r\n works in all cases except at the end of teh second mail function so I don't think using only \n will cure it.

 

As a further experiment I added another \r\n like this:-

$body=$day . "/" . $month . "/" . $year . ", " . $starttime . " to " . $endtime . "\r\n" . $room . ", " . $book . "\r\n" . $club . "\r\n" . $who . ", " . 
$phone . ", " . $email . "\r\n" . $message . "\r\n" . $who;

mail( $email, "Village Hall booking", "$day/$month/$year $starttime to $endtime\r\n$room $book\r\n$club\r\n$replymsg", "From: $admin" );

mail( $admin, "Village Hall booking", $body, "From: $admin" ); 

 

so there are five \r\n in the second mail. The first three work as before but the last two don't work so I have

day/month/year, starttime endtime

room, book

club

who, phone, email message who

 

when it should show

day/month/year, starttime endtime

room, book

club

who, phone, email

message

who

 

I'm beginning to think that there is a limit of three \r\n and more than that don't work.

 

Edit: after testing again I've discovered that the \r\n after $email doesn't work. I substituted $who for $email just before $message

$body=$day . "/" . $month . "/" . $year . ", " . $starttime . " to " . $endtime . "\r\n" . $room . ", 
" . $book . "\r\n" . $club . "\r\n" . $who . ", " . $phone . ", " . $who . "\r\n" . $message;

and all four \r\n worked. I also tested four \r\n in the first mail function which does not have $email and all four worked there, so for some reason $email blocks the operation of \r\n.

 

I thought that swapping $phone and $email

$body=$day . "/" . $month . "/" . $year . ", " . $starttime . " to " . $endtime . "\r\n" . $room . ", " . $book . "\r\n" . $club . "\r\n" . $who . ", " . $email . " " . $phone . "\r\n" . $message; 

so that there was a space after $email between $email and $phone and \r\n after $phone before $message would enable the last \r\n to work, but it didn't. However, as I said before, leaving out $email altogether does enable the fourth \r\n to work.

 

SUCCESS

EDIT 2: I've just used my online webmail system to view an email instead of using Outlook and all four \r\n worked properly, showing the message on a separate line as I wanted. So Outlook, for some reason, ignores \r\n when it occurs after an email address.

Edited by Wickham
Link to comment
Share on other sites

Wickham,

 

You might find this post helpful, it talks about Outlook and using (PHP_EOL) instead of \r\n....

... http://forums.devarticles.com/php-development-48/formatting-a-newline-line-break-in-php-html-output-5274.html

 

That article is mainly about HTML emails but mine are just plain text where I wanted line breaks. I've seen some topics that say that Outlook does have problems with \r\n and now I've added a double \r\n\r\n to the last position before the $message

$body=$day . "/" . $month . "/" . $year . ", " . $starttime . " to " . $endtime . "\r\n" . $room . ", " . $book . "\r\n" . $club . "\r\n" . $who . ", " . $email . " " . $phone . "\r\n\r\n" . $message;

which adds a full line space instead of a line break. It adds a full line space in Outlook and webmail so at least it's consistent and in my case it's quite a good idea to have a full line space between short data like an email address and a message.

 

What is strange is that other articles say that Outlook has a problem with all \r\n but in my case they all worked except in the one place when following an email address

Link to comment
Share on other sites

I found this in a php book last night.

 

"On some - primarily Unix-systems, the \r\n characters aren't handled properly. If you have problems with them, use just \n instead." Larry Ullman

 

I've also read that it's important to have an empty line separating the message(body) from the headers, using \r\n\r\n or just \n\n.

Edited by markvs
Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
×
×
  • Create New...