php 新行 (\r\n) 在电子邮件正文中不起作用

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16063312/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 10:28:10  来源:igfitidea点击:

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

phpemailnewlinecontent-type

提问by Verbatus

I am using PHP mail()function:

我正在使用 PHPmail()函数:

    $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-TypeHTTP header. How can I make new linesand still use my "Content-Type" declaration?


它在没有Content-TypeHTTP 标头的情况下工作正常。如何创建新行并仍然使用我的“内容类型”声明?

回答by errieman

You need to use a <br>because your Content-Typeis text/html.

您需要使用 a<br>因为您Content-Typetext/html

It works without the Content-Typeheader because then your e-mail will be interpreted as plain text. If you really want to use \nyou should use Content-Type: text/plainbut then you'll lose any markup.

它可以在没有Content-Type标题的情况下工作,因为这样您的电子邮件将被解释为纯文本。如果你真的想使用\n你应该使用Content-Type: text/plain但是你会失去任何标记。

Also check out similar question here.

也在这里查看类似的问题。

回答by Mark A.

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

如果您要发送 HTML 电子邮件,按照说明使用<BR>(或<BR /></BR>)。
如果您要发送纯文本电子邮件,请使用%0D%0A
\r = %0D(Ctrl+M = 回车)
\n = %0A(Ctrl+A = 换行)

如果您的电子邮件中有电子邮件链接,
例如

<A HREF="mailto?To=...&Body=Line 1%250D%250ALine 2">Send email</A>

Then use %250D%250A

然后使用%250D%250A

%25 = %

%25 = %

回答by Sanoob

You need to use <br>instead of \r\n. For this you can use built in function call nl2brSo your code should be like this

您需要使用<br>而不是\r\n. 为此,您可以使用内置函数调用nl2br所以您的代码应该是这样的

 $message = nl2br("CCCC\r\nCCCC CCCC \r CCC \n CCC \r\n CCC \n\r CCCC");

回答by Nut_Shot

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

如果你使用content-type: text/html你需要放一个,<br>因为你的消息会像一个html 文件一样受到威胁。

But if you change your content-typeto text/plaininstead of text/htmlyou will be able to use \r\ncharacters.

但是如果你改变你的content-typetotext/plain而不是text/html你将能够使用\r\n字符。

回答by Windkin

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>总是不够的。如果您不使用以下命令告诉 Outlook 它是一个自闭合 html 标记,MS Outlook 2007 将忽略它

 <BR />

回答by John

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

您可以使用%0A字符代码在文本/纯内容类型中添加新行字符。

For example:

例如:

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

Here is the jsfiddle

这是jsfiddle

回答by Arun Prasad E S

' '   

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

在我的情况下缺少空间,当添加的空格' \r\n'开始工作时

回答by Mike Q

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

另一件事使用“”,“\r\n”和“\r\n”之间存在差异。

回答by walter1957

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

如果在标题中使用content-type: text/plain.

Beware: If you do the Following php code:

当心:如果您执行以下 php 代码:

    $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:

您将在 Windows 浏览器中获得以下信息:

ab
cd
e
f

ab cd e f

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

content-type: text/plain在电子邮件输出中获得以下内容;

ab
cd
e
f

回答by DrOne

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

对于邮件函数中的文本/纯文本邮件,一定要使用 PHP_EOL 常量,您也可以将其与
文本/html 文本结合使用:

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