Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 12 of 12
  1. #1
    Regular Coder
    Join Date
    Aug 2009
    Location
    in .........
    Posts
    263
    Thanks
    183
    Thanked 0 Times in 0 Posts

    problem in mail() with \r\n

    I have a mail() function and I make it to allow users to send messages to each others,so the user is who write the content of the message.
    the problem in the \r \n characters,when the user write in a new line or take a space those characters show in his sent message to the other user with the other word.
    hope and one have a solution.

  • #2
    Super Moderator
    Join Date
    Feb 2009
    Location
    England
    Posts
    539
    Thanks
    8
    Thanked 63 Times in 54 Posts
    You shouldn't use \r\n in the message content (only the headers, if you're specifying some manually). You can fix this by wrapping your content with str_replace("\r\n", "\n", content)
    lamped.co.uk :: Design, Development & Hosting
    marcgray.co.uk :: Technical blog

  • Users who have thanked Lamped for this post:

    Mido™ (10-26-2009)

  • #3
    Master Coder
    Join Date
    Dec 2007
    Posts
    6,682
    Thanks
    436
    Thanked 890 Times in 879 Posts
    Quote Originally Posted by Mido™ View Post
    I have a mail() function and I make it to allow users to send messages to each others,so the user is who write the content of the message.
    the problem in the \r \n characters,when the user write in a new line or take a space those characters show in his sent message to the other user with the other word.
    hope and one have a solution.
    I don't understand what you mean. Can you give a example?

    best regards

  • Users who have thanked oesxyl for this post:

    Mido™ (10-26-2009)

  • #4
    Regular Coder
    Join Date
    Aug 2009
    Location
    in .........
    Posts
    263
    Thanks
    183
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by ComputerX View Post
    You shouldn't use \r\n in the message content (only the headers, if you're specifying some manually). You can fix this by wrapping your content with str_replace("\r\n", "\n", content)
    I think you may misunderstood me,I didn't use this \r\n or write them or the user who write the message write them, but when the user who write the message take a break and start writing in a new line the character "\n" appear in the sent message to who read it.

  • #5
    Regular Coder
    Join Date
    Aug 2009
    Location
    in .........
    Posts
    263
    Thanks
    183
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by oesxyl View Post
    I don't understand what you mean. Can you give a example?

    best regards
    ok,as I said the user is who write the content of the message and then it sent to the other user that he want to his email,so no one write this characters " \r \n " in the message but it appear in the sent message to who read it if who wrote it take a break and wrote in a new line.
    I want a solution to avoid this character and in the same time make who wrote in a new line appear in a new line after sending the message.

  • #6
    Super Moderator
    Join Date
    Feb 2009
    Location
    England
    Posts
    539
    Thanks
    8
    Thanked 63 Times in 54 Posts
    Oh, you don't mean your recipient is literally getting "\n" in their email, not a new line but like:

    Line 1\nLine2\nLine3

    When it should be
    Line 1
    Line 2
    Line 3

    If that's the case, you want stripslashes or stripcslashes:

    http://www.php.net/manual/en/function.stripslashes.php
    http://www.php.net/manual/en/function.stripcslashes.php
    lamped.co.uk :: Design, Development & Hosting
    marcgray.co.uk :: Technical blog

  • Users who have thanked Lamped for this post:

    Mido™ (10-26-2009)

  • #7
    Master Coder
    Join Date
    Dec 2007
    Posts
    6,682
    Thanks
    436
    Thanked 890 Times in 879 Posts
    Quote Originally Posted by Mido™ View Post
    ok,as I said the user is who write the content of the message and then it sent to the other user that he want to his email,so no one write this characters " \r \n " in the message but it appear in the sent message to who read it if who wrote it take a break and wrote in a new line.
    I want a solution to avoid this character and in the same time make who wrote in a new line appear in a new line after sending the message.
    sorry, I still don't understand.
    Anyway you can replace any \r, \n with a space before the user recive the message using this:
    PHP Code:
    $newmessage preg_replace("/[\r\n]/"" ",$oldmessage); 
    this will join the lines into a single one like this:
    Code:
    my message
    is on few
    lines
    become:
    Code:
    my message is on few lines
    .
    I didn't test it, so let's me know if work and if is what you need.

    best regards

  • Users who have thanked oesxyl for this post:

    Mido™ (10-26-2009)

  • #8
    Regular Coder
    Join Date
    Aug 2009
    Location
    in .........
    Posts
    263
    Thanks
    183
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by ComputerX View Post
    Oh, you don't mean your recipient is literally getting "\n" in their email, not a new line but like:

    Line 1\nLine2\nLine3

    When it should be
    Line 1
    Line 2
    Line 3

    If that's the case, you want stripslashes or stripcslashes:

    http://www.php.net/manual/en/function.stripslashes.php
    http://www.php.net/manual/en/function.stripcslashes.php
    exactly this is what I was meaning, but I used stripslashes and it didn't fix it.
    also I notice that this probelm happen when I use mysql_real_escape_string() function, but I need to use it.

  • #9
    Super Moderator
    Join Date
    Feb 2009
    Location
    England
    Posts
    539
    Thanks
    8
    Thanked 63 Times in 54 Posts
    We could use a little more information then. Maybe some code?
    lamped.co.uk :: Design, Development & Hosting
    marcgray.co.uk :: Technical blog

  • #10
    Supreme Master coder! abduraooft's Avatar
    Join Date
    Mar 2007
    Location
    N/A
    Posts
    14,865
    Thanks
    160
    Thanked 2,224 Times in 2,211 Posts
    Quote Originally Posted by Mido™ View Post
    exactly this is what I was meaning, but I used stripslashes and it didn't fix it.
    also I notice that this probelm happen when I use mysql_real_escape_string() function, but I need to use it.
    You should use that function only when you use the data from any external variable(GPC) in your mysql queries. The data being passed to the mail() function shouldn't be passed to that function before.
    The Dream is not what you see in sleep; Dream is the thing which doesn't let you sleep. --(Dr. APJ. Abdul Kalam)

  • Users who have thanked abduraooft for this post:

    Mido™ (10-26-2009)

  • #11
    Master Coder
    Join Date
    Dec 2007
    Posts
    6,682
    Thanks
    436
    Thanked 890 Times in 879 Posts
    Quote Originally Posted by Mido™ View Post
    exactly this is what I was meaning, but I used stripslashes and it didn't fix it.
    also I notice that this probelm happen when I use mysql_real_escape_string() function, but I need to use it.
    mysql_real_escape_string take as argument the string so in the place you are using you can that variable for mail and the result of the function to insert in your data base. This way you don't need to remove \r,\n from the message.

    best regards

  • Users who have thanked oesxyl for this post:

    Mido™ (10-26-2009)

  • #12
    Regular Coder
    Join Date
    Aug 2009
    Location
    in .........
    Posts
    263
    Thanks
    183
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by abduraooft View Post
    You should use that function only when you use the data from any external variable(GPC) in your mysql queries. The data being passed to the mail() function shouldn't be passed to that function before.
    I used it cause I thought it would give me more security,but you are right I don't need it.


  •  

    Posting Permissions

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