Jump to content

new lines are not appearing properly when using mail();


Recommended Posts

Here is a snippet of my code:

 

<?php
$contactname =  db_escape_string(htmlspecialchars($_POST['name']));
		$sentname = $user->name;
		$subject =   db_escape_string(htmlspecialchars($_POST['subject']));
		$body =   db_escape_string($_POST['body']);
//pulled in an earlier (not shown) query
$contactemail = $rowA['contactemail'];



$headers = 	'From: ' . $user->mail . ' ' . "\r\n" .
						'Reply-To: ' . $user->mail . ' ' . "\r\n" .
						'X-Mailer: PHP/' . phpversion();
			$headers  .= 'MIME-Version: 1.0' . "\r\n";
			//prevent mail from going to spam folder
			$header .= 'Return-Path: ' . $user->name .' < ' . $user->main . '>\r\n'; 
			$headers .= 'Content-Type: text/plain; charset="iso-8859-1"' . "\r\n";
			$mailbody = $body;
			$mailbody .= '\r\n- ------ \r\n Lorem ipsum';

			mail($contactemail, $subject, $mailbody, $headers);
?>

 

I'm actually not that familiar with this, so excuse any stupid oversights on my part.

The sending of mail works, however, I keep receiving this:

Hello\r\nThere are no new messages\r\n\r\n\r\n----\r\nLorem ipsum

 

Instead of

Hello

There are no new messages

----

Lorem ipsum

 

When entering a new line (through hitting the enter key, not actually using "\n") in my form.

 

I wondering what I'm doing wrong. Are my headers incorrect for what I want (simple text emails)?

Is it just my email that's reading it wrong (gmail)?

 

I tried setting my emails to HTML and using nl2br(); but the same problem occurred, but with "\r\n<br />" in the emails.

These lines are likely your issue:

$header .= 'Return-Path: ' . $user->name .' < ' . $user->main . '>\r\n';
$mailbody .= '\r\n- ------ \r\n Lorem ipsum';

 

It should be:

$header .= "Return-Path: " . $user->name ." < " . $user->main . ">\r\n";
$mailbody .= "\r\n- ------ \r\n Lorem ipsum";

 

Notice the use of " and not ' (you'll notice that everywhere else in your script, double quotes are used to encapsulate /r/n except on those lines)

 

The short answer: PHP looks at strings in single quotes differently than in double quotes, in single quotes PHP will always echo verbatim.  In double quotes PHP will evaluate the text rather than echo verbatim.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  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.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.