换行符在 PHP 邮件中不起作用
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3073382/
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
newline not working in PHP mail
提问by ChuckO
I'm using the following to send an email:
我正在使用以下内容发送电子邮件:
<php ....
$message = 'Hi '.$fname.', \r\n Your entries for the week of '
.$weekof.' have been reviewed. \r\n Please login and View Weekly reports to see the report and comments. \r\n Thanks, \r\n'.$myname;
mail($to, $subject, $message, $from);
?>
When the message is received it does not start a new line at the "\r\n" but just prints them as part of the message.
收到消息后,它不会在“\r\n”处开始新行,而只是将它们作为消息的一部分打印出来。
I only tried it in Thunderbird 3, not any other clients.
我只在 Thunderbird 3 中尝试过,没有在任何其他客户端中尝试过。
回答by Oren
Try to change your 'to "- php interprets a string inside single quotes as literals, whereas with quotes (") it will expand the \r\nto what you want.
尝试将您更改'为"- php 将单引号内的字符串解释为文字,而使用引号 ( ") 它将扩展\r\n为您想要的内容。
More information: http://www.php.net/manual/en/language.types.string.php#language.types.string.syntax.single
更多信息:http: //www.php.net/manual/en/language.types.string.php#language.types.string.syntax.single
回答by Harijs Krūtainis
Not an answer to the question, but may be of help to someone.
不是问题的答案,但可能对某人有所帮助。
Send a HTML message, and instead of \n, use <BR>.
发送 HTML 消息,而不是 \n,使用<BR>。
And use <PRE></PRE>or CSS for preformatted text, thus translating \n to actual new lines.
并<PRE></PRE>为预格式化文本使用或 CSS,从而将 \n 转换为实际的新行。
$headerFields = array(
"From: [email protected]",
"MIME-Version: 1.0",
"Content-Type: text/html;charset=utf-8"
);
mail($to, $subj, $msg, implode("\r\n", $headerFields));
回答by kayleighsdaddy
<?php ....
$message = "Hi ".$fname.", \r\n Your entries for the week of "
.$weekof." have been reviewed. \r\n Please login and View Weekly reports to see the report and comments. \r\n Thanks, \r\n".$myname;
mail($to, $subject, $message, $from);
?>

