电子邮件-换行符在PHP mai中不起作用

我正在使用以下方法发送电子邮件:

<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);
?>

收到消息后,它不会在“ \ r \ n”处开始新行,而只是将其打印为消息的一部分。

我只在Thunderbird 3中尝试过,没有其他任何客户端。

ChuckO asked 2020-06-30T05:17:42Z
3个解决方案
109 votes

尝试将'更改为2945350525979945536-php将单引号内的字符串解释为文字,而使用引号(")会将\r\n扩展为所需的内容。

详细信息:[http://www.php.net/manual/en/language.types.string.php#language.types.string.syntax.single]

Oren answered 2020-06-30T05:18:01Z
5 votes

不是问题的答案,但可能对某人有所帮助。

发送HTML消息,而不是\ n,请使用<PRE></PRE>

并对预格式化的文本使用<PRE></PRE>或CSS,从而将\ n转换为实际的新行。

$headerFields = array(
    "From: who@are.you",
    "MIME-Version: 1.0",
    "Content-Type: text/html;charset=utf-8"
    );
mail($to, $subj, $msg, implode("\r\n", $headerFields));
Harijs Krūtainis answered 2020-06-30T05:18:30Z
2 votes
<?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);
?>
kayleighsdaddy answered 2020-06-30T05:18:45Z
translate from https://stackoverflow.com:/questions/3073382/newline-not-working-in-php-mail