New lines (\r\n) are not working in email body

Verbatus picture Verbatus · Apr 17, 2013 · Viewed 157.1k times · Source

I am using PHP mail() function:

    $to      = 'AAAA <[email protected]>';
    $subject = 'BBBB';
    $message = "CCCC\r\nCCCC CCCC \r CCC \n CCC \r\n CCC \n\r CCCC";
    $headers = 'From: DDD<[email protected]>' . "\r\n";
    $headers .= "Content-Type: text/html; charset=\"UTF-8\"; format=flowed \r\n";
    $headers .= "Mime-Version: 1.0 \r\n"; 
    $headers .= "Content-Transfer-Encoding: quoted-printable \r\n";
    mail($to, $subject, $message, $headers);

When I receive this email it looks like this:

CCCC CCCC CCCC CCC CCC CCC CCCC

I would expect something like this:

CCCC
CCCC CCCC CCC 
CCC 
CCC 
CCCC


It works fine without Content-Type HTTP header. How can I make new lines and still use my "Content-Type" declaration?

Answer

errieman picture errieman · Apr 17, 2013

You need to use a <br> because your Content-Type is text/html.

It works without the Content-Type header because then your e-mail will be interpreted as plain text. If you really want to use \n you should use Content-Type: text/plain but then you'll lose any markup.

Also check out similar question here.