PHP new line break in emails

后端 未结 8 1707
闹比i
闹比i 2020-11-27 20:35

I have the following code:

$message = \"Good news! The item# $item_number on which you placed a bid of \\$ $bid_price is now available for purchase at your b         


        
相关标签:
8条回答
  • 2020-11-27 21:16

    if you are outputting the code as html - change /n -->
    and do echo $message;

    0 讨论(0)
  • 2020-11-27 21:16

    When we insert any line break with a programming language the char code for this is "\n". php does output that but html can't display that due to htmls line break is
    . so easy way to do this job is replacing all the "\n" with "
    ". so the code should be

    str_replace("\n","<br/>",$str);
    

    after adding this code you wont have to use pre tag for all the output oparation.

    copyed this ans from this website :

    0 讨论(0)
  • 2020-11-27 21:17

    EDIT: Maybe your class for sending emails has an option for HTML emails and then you can use <br />

    1) Double-quotes

    $output = "Good news! The item# $item_number  on which you placed a bid of \$ $bid_price is now available for purchase at your bid price.\nThe seller, $bid_user is making this offer.\n\nItem Title : $title\n\nAll the best,\n $bid_user\n$email\n";
    

    If you use double-quotes then \n will work (there will be no newline in browser but see the source code in your browser - the \n characters will be replaced for newlines)

    2) Single quotes doesn't have the effect as the double-quotes above:

    $output = 'Good news! The item# $item_number  on which you placed a bid of \$ $bid_price is now available for purchase at your bid price.\nThe seller, $bid_user is making this offer.\n\nItem Title : $title\n\nAll the best,\n $bid_user\n$email\n';
    

    all characters will be printed as is (even variables!)

    3) Line breaks in HTML

    $html_output = "Good news! The item# $item_number  on which you placed a bid of <br />$ $bid_price is now available for purchase at your bid price.<br />The seller, $bid_user is making this offer.<br /><br />Item Title : $title<br /><br />All the best,<br /> $bid_user<br />$email<br />";
    
    • There will be line breaks in your browser and variables will be replaced with their content.
    0 讨论(0)
  • 2020-11-27 21:19

    Are you building this string using single or double quotes? \r and \n only work with double quotes, as well as embedded variables. For example:

    $foo = 'bar';
    echo 'Hello \n $foo!';
    

    will output:

    Hello \n $foo!
    

    But:

    $foo = 'bar';
    echo "Hello \n $foo!";
    

    will output:

    Hello
    bar!
    
    0 讨论(0)
  • 2020-11-27 21:19

    If you output to html or an html e-mail you will need to use <br> or <br /> instead of \n.

    If it's just a text e-mail: Are you perhaps using ' instead of "? Although then your values would not be inserted either...

    0 讨论(0)
  • 2020-11-27 21:26

    you can use "Line1<br>Line2"

    0 讨论(0)
提交回复
热议问题