john
john

Reputation: 41

How comes the message I am receving from my php mailer adds random \r\n\r\n to my messages?

I have noticed this issue, that when I enter paragraphs in the message being sent from my textarea from the website, I receive the message well but somehow in the paragraphs

\r\n\r\n

is added in the spaces of the paragraphs. Below is an example of a mail I have received from the form:

Our Passion leads to design, design leads to performance, performance leads to success. We believe that apps and websites should not only be eye catching but actually provide a great user experience that users will remember.\r\[email protected]\r\n4th Floor, Building\r\nCountry\r\n+222222

When the message I entered is:

Our Passion leads to design, design leads to performance, performance leads to success. We believe that apps and websites should not only be eye catching but actually provide a great user experience that users will remember.

[email protected]

4th Floor, Building Country

+2222222

Here is my mail PHP code:

 <?php

$name = $_POST['sname'];
$email = $_POST['lemail'];
$message = $_POST['lmessage'];



$name = mysqli_real_escape_string($db, $name);
$email = mysqli_real_escape_string($db, $email);
$message = mysqli_real_escape_string($db, $message);

$to      = '[email protected]';
$subject = 'Submission from World Contact';


$main = '
<html>
<head>
<title>World</title>
</head>
<body>
<h1>Submission from World Contact</h1>
<h4>New Message in World Contact</h4>
<table cellspacing="0" cellpadding="10" style="width: 400px; height: auto;">
<tr>
<th style="width: 150px; vertical-align: top; text-align: left">Stage Name:</th><td> '.$name.'</td>
</tr>
<tr style="width: 150 vertical-align: top; text-align: left; background-color: #e0e0e0;">
<th>Email:</th><td> '.$email.'</td>
</tr>
<tr>
<th style="width: 150px; vertical-align: top; text-align: left">Message:</th><td> '.$message.' </td>
</tr>
</table>
</body>
</html>';

$headers = "MIME-Version: 1.0" . "\n";
$headers .= "Content-type:text/html;charset=UTF-8" . "\n";
$headers .= 'From: Signups' . "\n" .
'Reply-To: [email protected]' . "\n" .
'X-Mailer: PHP/' . phpversion();

mail($to, $subject, $main, $headers);

?>

Upvotes: 0

Views: 122

Answers (3)

Don&#39;t Panic
Don&#39;t Panic

Reputation: 41810

Don't use mysqli_real_escape_string on those values before using them in the email. That function is intended to escape values for input into a database. It is adding the visible \n and \r.

From the documentation for that function:

Characters encoded are NUL (ASCII 0), \n, \r, \, ', ", and Control-Z.

Try this example:

$example = 'something
something else';

var_dump($example);
$example = mysqli_real_escape_string($db, $example);
var_dump($example);

If you want to preserve the line breaks, use $message = nl2br($message) without using mysqli_real_escape_string, then send the email, then do what you need to do to get the value into your database.

Upvotes: 2

kzhao14
kzhao14

Reputation: 2630

When you submit "new lines" to PHP, they are replaced by \n and \r. However, when you email it, most email clients read in HTML meaning \n and \r have no meaning except as text, so they are printed. However, if you want to use new lines, use the PHP function nl2br() (New line to br), as follows:

$message=nl2br($message);

Also, you don't need to use mysqli_real_escape_string() as that prepares a string for SQL insertion, not emails.

Upvotes: 0

mcv
mcv

Reputation: 1460

Has suggested by AbaCadaver use nl2br()

PHP nl2br()

Pass in your string $message

nl2br() changes \n and \r to <br/>

Upvotes: 0

Related Questions