• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 292
  • Last Modified:

php mail - endline not working correctly

within the email being retrieved in the email, i am getting results like \r\n within the content.  I have read online where people are suggesting using double quotes.  The problem is the value is of the message is being retrieved from post.  How can i solve this problem?
//this is not the exact code... i dont have access to the code at the moment
$to = 'example@yahoo.com';
$headers = 'From: webmaster@example.com' . "\r\n" .
    'Reply-To: webmaster@example.com' . "\r\n" .
    'X-Mailer: PHP/' . phpversion();
$msg = isset($_POST['msg']) ? mysql_escape_string($_POST['msg']) : " ";
<form method='post' name='iform'>
     <label for='msg'>
     <textarea name='msg'></textarea>
 mail($to, $subject,  $msg, $headers);

Open in new window

  • 2
  • 2
2 Solutions
Roger BaklundCommented:
Don't use mysql_escape_string(). This function is for escaping strings for use in SQL statements.
Mark GilbertSenior Performance EngineerCommented:
Can I suggest that you look at the phpmailer classes instead of using the mail function.


The mail function is very powerful in what it can handle but can be a real pain to get all the elements of what you are trying to achieve to meld together error free. the phpmailer classes help you greatly with configurations for all kinds of mail servers, as well as easy to use examples to do plain text emails, html, html with inline attachments, etc. The examples are very easy to follow too.

Hope this helps.
Roger BaklundCommented:
The asker got \r\n characters in his email, mysql_escape_string() creates such characters. It is very likely the reason for this error, and the correct advise is to not use this function for email messages. In my opinion my answer http:#a24805179 was correct.
Mark GilbertSenior Performance EngineerCommented:
Angel, recommended disposition is a split between cxr and ingwa. The mysql_escape_string causes issues as stated by cxr and the phpmailer classes work to ensure emails are correctly formatted when used correctly.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now