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
' '
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
Source: Stackoverflow.com