Search code examples
phpcarriage-return

Blank line gets removed in HTML mail -> how to add <br> after blank lines


In order to maintain formatting in an HTML mail, I'm looking for some php code to change a blank line into <br>.

For example, the original text:

Line 1
Line 2

Line 3

Should turn into:

Line 1
Line 2
< br >
Line 3

I tried nl2br, but that functions add <br> after every line, not just the blank lines.

Any help would be appreciated.

David


Solution

  • I analysed the message contents byte by byte and found a solution.

    To maintain a line break in an HTML email, use the code below:

    $TheMessage=str_replace("\r\n\r\n", "<br>",$TheMessage);
    

    It is the double 'carriage return & line feed' right after each other that is the key to the solution.