I am using PHP mail()
function:
$to = 'AAAA <postmaster@xxx.xx>';
$subject = 'BBBB';
$message = "CCCC
CCCC CCCC CCC
CCC
CCC
CCCC";
$headers = 'From: DDD<postmaster@xxx.xx>' . "
";
$headers .= "Content-Type: text/html; charset=\"UTF-8\"; format=flowed
";
$headers .= "Mime-Version: 1.0
";
$headers .= "Content-Transfer-Encoding: quoted-printable
";
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?
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 you should use
Content-Type: text/plain
but then you'll lose any markup.
Also check out similar question here.
You need to use <br>
instead of . For this you can use built in function call nl2br So your code should be like this
$message = nl2br("CCCC
CCCC CCCC CCC
CCC
CCC
CCCC");
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
= %0D (Ctrl+M = carriage return)
= %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 = %
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 characters.
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 />
This worked for me.
$message = nl2br("
===============================
www.domain.com
===============================
From: ".$from."
To: ".$to."
Subject: ".$subject."
Message: ".$_POST['form-message']);
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
' '
space was missing in my case, when a blank space added ' '
started to work
Another thing use "", there is a difference between " " and ' '.
" " produces 2 new lines while " ","" & " " 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>',"
",$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
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";