PHP-新行(\ r \ n)在电子邮件正文中不起作用
我正在使用PHP Content-Type功能:
$to = 'AAAA ';
$subject = 'BBBB';
$message = "CCCC\r\nCCCC CCCC \r CCC \n CCC \r\n CCC \n\r CCCC";
$headers = 'From: DDD' . "\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);
当我收到这封电子邮件时,它看起来像这样:
CCCC CCCC CCCC CCC CCC CCC CCCC
我期望这样的事情:
CCCC
CCCC CCCC CCC
CCC
CCC
CCCC
它在没有Content-Type HTTP标头的情况下可以正常工作。 如何制作新行并仍然使用“ Content-Type”声明?
11个解决方案
93 votes
您需要使用Content-Type,因为您的\n是Content-Type: text/plain。
它没有Content-Type标头就可以工作,因为这样您的电子邮件将被解释为纯文本。 如果您确实要使用\n,则应该使用Content-Type: text/plain,但那样会丢失任何标记。
也可以在这里查看类似的问题。
errieman answered 2020-01-19T10:37:36Z
23 votes
如果要发送HTML电子邮件,请按照说明使用
(或
或)。
如果您要发送纯文本电子邮件,请使用%0D%0A
\ r =%0D(Ctrl + M =回车)
\ n =%0A(Ctrl + A =换行符)
如果您的电子邮件中有电子邮件链接,
例如
然后使用%250D%250A
%25 =%
Mark A. answered 2020-01-19T10:38:26Z
7 votes
您需要使用
而不是\r\n。 为此,您可以使用内置函数调用nl2br所以你的代码应该是这样的
$message = nl2br("CCCC\r\nCCCC CCCC \r CCC \n CCC \r\n CCC \n\r CCCC");
Sanoob answered 2020-01-19T10:38:46Z
4 votes
如果您使用content-type,则需要放置text/plain,因为您的消息将像html文件一样受到威胁。
但是,如果您将content-type改为text/plain(而不是text/html),则可以使用\r\n字符。
Nut_Shot answered 2020-01-19T10:39:11Z
2 votes
使用
并不总是足够的。 如果您不通过使用以下命令告诉Outlook这是一个自动关闭的html标签,MS Outlook 2007将忽略此操作
Windkin answered 2020-01-19T10:39:31Z
1 votes
您可以使用%0A字符代码在文本/普通内容类型中添加换行符。
例如:
这是jsfiddle
John answered 2020-01-19T10:39:59Z
0 votes
这对我有用。
$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']);
Naiguel Developer answered 2020-01-19T10:40:19Z
0 votes
' '
在我的情况下,缺少空间时,添加的空白' \r\n'开始工作
Arun Prasad E S answered 2020-01-19T10:40:39Z
0 votes
另一件事使用“”,“ \ r \ n”和“ \ r \ n”之间有区别。
Mike Q answered 2020-01-19T10:40:59Z
0 votes
如果在标题中使用content-type: text/plain,则“ \ n \ r”产生2行,而“ \ n”,“ \ r”和“ \ r \ n”产生单行。
当心:如果您执行以下php代码:
$message='ab
cd
e
f';
print $message.'
';
$message=str_replace('
',"\r\n",$message);
print $message;
您可以在Windows浏览器中获得以下信息:
ab
cd
e
f
ab cd e f
使用content-type: text/plain,您将在电子邮件输出中获得以下内容;
ab
cd
e
f
walter1957 answered 2020-01-19T10:41:32Z
0 votes
对于邮件功能中的纯文本/纯文本邮件,绝对使用PHP_EOL常量,您可以将其与
对于text / html文本也是如此:
$messagePLAINTEXT="This is my message."
. PHP_EOL .
"This is a new line in plain text";
$messageHTML="This is my message."
. PHP_EOL . "
" .
"This is a new line in html text, check line break in code view";
$messageHTML="This is my message."
. "
" .
"This is a new line in html text, no line break in code view";
DrOne answered 2020-01-19T10:41:57Z