63

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?

informatik01
  • 15,174
  • 9
  • 67
  • 100
Verbatus
  • 733
  • 1
  • 5
  • 10
  • 6
    text/html !== plain text - if you want new lines in text/html, you use `
    `
    – Mark Baker Apr 17 '13 at 14:53
  • I used text/plain instead text/html and everything works as expected. thank you! – Verbatus Apr 17 '13 at 15:04
  • possible duplicate of [Newline showing up on screen but not in email](http://stackoverflow.com/questions/3578174/newline-showing-up-on-screen-but-not-in-email) – KatieK May 20 '14 at 20:25
  • My problem was that outlook decided that 'extra line breaks in this message was removed' – Thomas Oct 12 '17 at 09:49

13 Answers13

117

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.

informatik01
  • 15,174
  • 9
  • 67
  • 100
errieman
  • 1,570
  • 1
  • 10
  • 9
28

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

MeSo2
  • 282
  • 1
  • 2
  • 17
Mark A.
  • 281
  • 3
  • 2
8

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");
Sanoob
  • 2,328
  • 3
  • 26
  • 34
5

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.

Nut_Shot
  • 51
  • 1
  • 1
4

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 />
Windkin
  • 53
  • 4
  • 1
    This is still very true today 30 Dec 2019! Thanks for the posting. I was scratching my head for a few minutes and then found this!! And for some already following all recommendations this is the solution! It is a shame that
    format is not universally followed!
    – Nandostyle Dec 30 '19 at 18:42
2

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

For example:

<a href="mailto:someone@example.com?subject=Hello%20again&body=HI%20%0AThis%20is%20a%20new%20line"/>

Here is the jsfiddle

John
  • 8,104
  • 4
  • 45
  • 82
1

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

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

Arun Prasad E S
  • 7,342
  • 6
  • 61
  • 75
0

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

Mike Q
  • 5,006
  • 2
  • 41
  • 53
0

"\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
walter1957
  • 39
  • 1
  • 10
0

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";
DrOne
  • 51
  • 5
0

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.

Kar.ma
  • 603
  • 6
  • 12
0
$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>';
Martin Brisiak
  • 2,957
  • 12
  • 32
  • 48