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

PhpEmailNewlineContent Type

Php Problem Overview


I am using PHP mail() function:

    $to      = 'AAAA <postmaster@xxx.xx>';
    $subject = 'BBBB';
    $message = "CCCC\r\nCCCC CCCC \r CCC \n CCC \r\n CCC \n\r CCCC";
    $headers = 'From: DDD<postmaster@xxx.xx>' . "\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?

Php Solutions


Solution 1 - Php

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.

Solution 2 - Php

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 = %

Solution 3 - Php

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");

Solution 4 - Php

If you use content-type: text/html you need to put a <br> because your message will be threated like an html file.

But if you change your content-type to text/plain instead of text/html you will be able to use \r\n characters.

Solution 5 - Php

Using <BR> is not allways enough. MS Outlook 2007 will ignore this if you dont tell outlook that it is a selfclosing html tag by using

 <BR />

Solution 6 - Php

You can add new line character in text/plain content type using %0A character code.

For example:

<a href="mailto:[email protected]?subject=Hello%20again&body=HI%20%0AThis%20is%20a%20new%20line"/>

Here is the jsfiddle

Solution 7 - Php

This worked for me.

$message  = nl2br("
===============================\r\n
www.domain.com \r\n
===============================\r\n
From: ".$from."\r\n
To: ".$to."\r\n
Subject: ".$subject."\r\n
Message: ".$_POST['form-message']);

Solution 8 - Php

' '   

space was missing in my case, when a blank space added ' \r\n' started to work

Solution 9 - Php

Another thing use "", there is a difference between "\r\n" and '\r\n'.

Solution 10 - Php

"\n\r" produces 2 new lines while "\n","\r" & "\r\n" produce single lines if, in the Header, you use content-type: text/plain.

Beware: If you do the Following php code:

    $message='ab<br>cd<br>e<br>f';
print $message.'<br><br>';
    $message=str_replace('<br>',"\r\n",$message);
print $message;

you get the following in the Windows browser:

ab
cd
e
f

ab cd e f

and with content-type: text/plain you get the following in an email output;

ab
cd
e
f

Solution 11 - Php

for text/plain text mail in a mail function definitely use PHP_EOL constant, you can combine it with
too for text/html text:

$messagePLAINTEXT="This is my message."
. PHP_EOL .
"This is a new line in plain text";

$messageHTML="This is my message."
. PHP_EOL . "<br/>" .
"This is a new line in html text, check line break in code view";

$messageHTML="This is my message."
. "<br/>" .
"This is a new line in html text, no line break in code view";

Solution 12 - Php

OP's problem was related with HTML coding. But if you are using plain text, please use "\n" and not "\r\n".

My personal use case: using mailx mailer, simply replacing "\r\n" into "\n" fixed my issue, related with wrong automatic Content-Type setting.

Wrong header:

User-Agent: Heirloom mailx 12.4 7/29/08
MIME-Version: 1.0
Content-Type: application/octet-stream
Content-Transfer-Encoding: base64

Correct header:

User-Agent: Heirloom mailx 12.4 7/29/08
MIME-Version: 1.0
Content-Type: text/plain; charset=us-ascii
Content-Transfer-Encoding: 7bit

I'm not saying that "application/octet-stream" and "base64" are always wrong/unwanted, but they where in my case.

Solution 13 - Php

$mail = new PHPMailer;
$mail->isSMTP(); 
$mail->isHTML(true);

Insert this code after working all html tag like

<br> <p> in $mail->Body='Hello<br> how are you ?<b>';

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionVerbatusView Question on Stackoverflow
Solution 1 - PhperriemanView Answer on Stackoverflow
Solution 2 - PhpMark A.View Answer on Stackoverflow
Solution 3 - PhpSanoobView Answer on Stackoverflow
Solution 4 - PhpNut_ShotView Answer on Stackoverflow
Solution 5 - PhpWindkinView Answer on Stackoverflow
Solution 6 - PhpJohnView Answer on Stackoverflow
Solution 7 - PhpNaiguel DeveloperView Answer on Stackoverflow
Solution 8 - PhpArun Prasad E SView Answer on Stackoverflow
Solution 9 - PhpMike QView Answer on Stackoverflow
Solution 10 - Phpwalter1957View Answer on Stackoverflow
Solution 11 - PhpDrOneView Answer on Stackoverflow
Solution 12 - PhpKar.maView Answer on Stackoverflow
Solution 13 - PhpSushil SihagView Answer on Stackoverflow