Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 872
Verbatus Avatar asked Apr 17 '13 14:04

Verbatus


People also ask

How do you insert a new line in an email?

In your Send Outlook Mail Message activity you have selected the IsBodyHtml in the property as True, so you can use "<br />" tag where you need NewLine. This is a html tag for a new line.

Why is Outlook removing extra line breaks?

By default, the Auto Remove Line Breaks feature in Outlook is enabled. This causes the line breaks to be removed. Any two or more successive line breaks are not removed.

How do you insert a line break in plain text email?

You can insert a break into plaintext emails using {! BR()} .

Why is Outlook stripping line breaks from plain text emails?

Reason. By default, the Remove extra line breaks in plain text messages option is enabled in Outlook. So, if a line break is found, it is removed (but, if there are two or more successive line breaks, they are not removed).


3 Answers

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.

like image 167
errieman Avatar answered Oct 22 '22 05:10

errieman


If you are sending HTML email then use <BR> (or <BR />, or </BR>) as stated.
If you are sending a plain text email then use %0D%0A
\r = %0D (Ctrl+M = carriage return)
\n = %0A (Ctrl+A = line feed)

If you have an email link in your email,
EG

<A HREF="mailto?To=...&Body=Line 1%250D%250ALine 2">Send email</A>

Then use %250D%250A

%25 = %

like image 40
Mark A. Avatar answered Oct 22 '22 06:10

Mark A.


You need to use <br> instead of \r\n . For this you can use built in function call nl2br So your code should be like this

 $message = nl2br("CCCC\r\nCCCC CCCC \r CCC \n CCC \r\n CCC \n\r CCCC");
like image 10
Sanoob Avatar answered Oct 22 '22 06:10

Sanoob