SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Wizard WorldNews's Avatar
    Join Date
    Nov 2007
    Posts
    1,033
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)

    How to remove \r\n\r\n\r\n from emails?

    Hello,

    When I send an email in Php which email was generated based on a form textarea the email is full of \r\n\r\n wherever a new line was entered by the sender of the message!

    How does one remove these r/n in a Php generated email, I mean via this:
    mail($email, $subject, $message, $headers);

    and have normal break/new lines instead in the body of the email?

    Regards,

    Anoox search engine volunteer

    www.anoox.com

  2. #2
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,712
    Mentioned
    102 Post(s)
    Tagged
    4 Thread(s)
    It seems that the content of the email is being escaped incorrectly. The solution is not to remove them after the fact, but to work back to where the problem begins, and correctly deal with it there and then.

    If $message contains "Line one.\r\nLine two." you won't see the \r\n. It's only when $message is escaped, either incorrectly or multiply, that you see the problem.

    What does var_dump($message) show, and how far up through the code do you need to go before a change in $message occurs?
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript

  3. #3
    SitePoint Wizard WorldNews's Avatar
    Join Date
    Nov 2007
    Posts
    1,033
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Hi Paul,

    I figured out the problem.
    The message which is a POST from a form TEXTAREA was being done this: mysql_real_escape_string
    for insertion into the MySQL table. This action was introducing the r/ns for each new line. So now I am creating 2 version of the body of the message, one that is included in the email without the mysql_real_escape_string and one that is inserted into the MySQL table with the mysql_real_escape_string.

    Thanks anyway


    Quote Originally Posted by pmw57 View Post
    It seems that the content of the email is being escaped incorrectly. The solution is not to remove them after the fact, but to work back to where the problem begins, and correctly deal with it there and then.

    If $message contains "Line one.\r\nLine two." you won't see the \r\n. It's only when $message is escaped, either incorrectly or multiply, that you see the problem.

    What does var_dump($message) show, and how far up through the code do you need to go before a change in $message occurs?

    Anoox search engine volunteer

    www.anoox.com

  4. #4
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,712
    Mentioned
    102 Post(s)
    Tagged
    4 Thread(s)
    Quote Originally Posted by WorldNews View Post
    So now I am creating 2 version of the body of the message, one that is included in the email without the mysql_real_escape_string and one that is inserted into the MySQL table with the mysql_real_escape_string.
    That's where it helps to escape at the very last moment, as is demonstrated in the mysql_query examples.

    The message really should be properly escaped for each of its intended outputs. When outputting the message to the email, is there a proper way to correctly escape the message? It seems that there should be.
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript

  5. #5
    SitePoint Wizard WorldNews's Avatar
    Join Date
    Nov 2007
    Posts
    1,033
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Well I tell you I ma having the darnest of time with this outputting of the message properly.

    That is the message is displaying Ok in email, that is with exact number of line spacing, but in the Web page the line spacing is reduced by 1/2 half.

    So 1 line spacing is shown with NO line spacing and 2 as 1.

    And as noted now I am nl2br() the message when shown on the web page but sending the not nl2br() ed version via the email.

    To be exact this is not a critical problem, but very annoying putting it mildly. I mean if you have 1 line spacing between 2 paragraph, you want that to show.

    So for example if message is entered by user like this:

    paragraph1

    paragraph2


    paragraph3

    after I retrieve this message from MySQL to display on the web page, then it displays it like

    paragraph1
    paragraph2

    paragraph3


    Any ideas?

    ThanX.

    Quote Originally Posted by pmw57 View Post
    That's where it helps to escape at the very last moment, as is demonstrated in the mysql_query examples.

    The message really should be properly escaped for each of its intended outputs. When outputting the message to the email, is there a proper way to correctly escape the message? It seems that there should be.

    Anoox search engine volunteer

    www.anoox.com


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •