cratto
cratto

Reputation: 57

\r\n showing in my email when there is a line break

I'm not sure what I am doing wrong here. In the emails, \r\n keeps showing every time there is a line break. What do I need to modify in this code to fix it.

    public function sendSupportEmail($email, $name, $comments)
{
    //Wait until Google Apps are configured to accept from this domain
    //$to = "[email protected]";
    $to = "[email protected]";
    $subject = "Support: Support Inquiry";


    //Headers
    // To send HTML mail, you can set the Content-type header.
    $autoHeaders  = "MIME-Version: 1.0\r\n";
    $autoHeaders .= "Content-type: text/html; charset=iso-88591\r\n";
    $autoHeaders .= "From: Web Bot";
    $autoHeaders .= "<[email protected]>\r\n";
    $autoHeaders .= "Reply-To: [email protected]\r\n";
    $autoHeaders .= "Return-Path: [email protected]\r\n";
    $autoHeaders .= "X-Mailer: PHP 5.x\r\n";


    //Print the local date
    $date = new DateTime('now', new DateTimeZone('America/Denver'));
    $datePrint =  $date->format('F j, Y, g:i a');

    //Create Text Based Message Below
    $message = "<h3>Support Inquiry sent on {$datePrint}</h3>";
    $message .= "<b>Name:</b><br>{$name}<br><br>";
    $message .= "<b>Email:</b><br><a href='mailto:{$email}'>{$name}</a><br><br>";
    $message .= "<b>Comments:</b><p>{$comments}</p>";

    //Send them the E-Mail
    return mail($to, $subject, $message, $autoHeaders);
}

Upvotes: 0

Views: 1051

Answers (3)

James Osguthorpe
James Osguthorpe

Reputation: 171

I had same issue, turns out using mysqli_real_escape_string was causing it.

Upvotes: 1

Mike
Mike

Reputation: 1988

\n\r codes are going to be invisible in an Email client, regardless of whether or not you are rendering HTML or Text-based email. Look at, for instance, the Source Code of this website, and that of Google. Anything that is on a new line, technically, has a \n\r at the end.

\n\r says, to the text renderer "Line-Feed, Carriage Return," which harks back to terminals, and essentially says "Cursor down, Return cursor to start of line," much like on a typewriter.

In straight Text documents, this behaves as you would expect. However, as HTML is a markup-language, there are language codes that do this instead, and inner line-feeds have no effect.

I say all of the above to point out one specific thing: If there are ACTUAL line-feeds and carriage returns in your HTML document, you would not see them, as they are not the actual text '\n\r', \n and \r are textual representations of control characters.

So, all of that said, why would you see these? If you actually had the text '\n\r' in the document, and not the control characters. This can happen a few ways, most notably, misunderstanding the difference between " and '. " interprets the text inside of it, expanding control characters, variable references, etc, while ' does not:

$foo = 'bar';

echo "$foo"; // 'bar'
echo '$foo'; // '$foo'
echo "\n";   // actual line-feed
echo '\n';   // the text '\n'

See here for an example.

It is my guess that the actual contents of $comments contains the actual text '\n\r' and not the Line-feed + Carriage Return control characters. Either the user is actually entering the characters '\n\r' or you are inserting them somewhere. Look in your code and be sure you are using double-quotes anywhere you are trying to use control characters.

While I suggest cleaning your code to ensure that you are not inserting the text "\n\r" yourself, if it is actually the USER inserting these characters, you can clean them using this method.

Upvotes: 0

huysentruitw
huysentruitw

Reputation: 28091

For the headers ($autoHeaders), \r\n is the correct way to separate various fields. But the $comments itself needs to use the HTML <br>-tag to denote a line-break, because you're sending a HTML e-mail (see Content-Type).

You can use the nl2br function for that:

$message = "<h3>Support Inquiry sent on {$datePrint}</h3>";
$message .= "<b>Name:</b><br>{$name}<br><br>";
$message .= "<b>Email:</b><br><a href='mailto:{$email}'>{$name}</a><br><br>";
$message .= "<b>Comments:</b><p>{" . nl2br($comments) . "}</p>";
return mail($to, $subject, $message, $autoHeaders);

In addition you can use htmlentities() on $comments before nl2br to convert other characters to HTML entities (like &euro; for € f.e.):

$message = "<h3>Support Inquiry sent on {$datePrint}</h3>";
$message .= "<b>Name:</b><br>{$name}<br><br>";
$message .= "<b>Email:</b><br><a href='mailto:{$email}'>{$name}</a><br><br>";
$message .= "<b>Comments:</b><p>{" . nl2br(htmlentities($comments)) . "}</p>";
return mail($to, $subject, $message, $autoHeaders);

See Ideone sample

Upvotes: 0

Related Questions