[php] 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?

This question is related to php email newline content-type

The answer is


' '   

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


$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>';

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


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


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 />

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

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']);

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.


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


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

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.


"\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

Examples related to php

I am receiving warning in Facebook Application using PHP SDK Pass PDO prepared statement to variables Parse error: syntax error, unexpected [ Preg_match backtrack error Removing "http://" from a string How do I hide the PHP explode delimiter from submitted form results? Problems with installation of Google App Engine SDK for php in OS X Laravel 4 with Sentry 2 add user to a group on Registration php & mysql query not echoing in html with tags? How do I show a message in the foreach loop?

Examples related to email

Monitoring the Full Disclosure mailinglist require(vendor/autoload.php): failed to open stream Failed to authenticate on SMTP server error using gmail Expected response code 220 but got code "", with message "" in Laravel How to to send mail using gmail in Laravel? Laravel Mail::send() sending to multiple to or bcc addresses Getting "The remote certificate is invalid according to the validation procedure" when SMTP server has a valid certificate How to validate an e-mail address in swift? PHP mail function doesn't complete sending of e-mail How to validate email id in angularJs using ng-pattern

Examples related to newline

How can I insert a line break into a <Text> component in React Native? Print "\n" or newline characters as part of the output on terminal Using tr to replace newline with space How to write one new line in Bitbucket markdown? Line break in SSRS expression How to insert a new line in Linux shell script? Replace CRLF using powershell How to write new line character to a file in Java What is the newline character in the C language: \r or \n? How to print values separated by spaces instead of new lines in Python 2.7

Examples related to content-type

Passing headers with axios POST request Spring MVC 4: "application/json" Content Type is not being set correctly What are all the possible values for HTTP "Content-Type" header? New lines (\r\n) are not working in email body HTML Input="file" Accept Attribute File Type (CSV) How do you set the Content-Type header for an HttpClient request? Utility of HTTP header "Content-Type: application/force-download" for mobile? Set Content-Type to application/json in jsp file Cannot set content-type to 'application/json' in jQuery.ajax Difference between application/x-javascript and text/javascript content types