【问题标题】:newline not working in PHP mail换行符在 PHP 邮件中不起作用
【发布时间】:2011-03-05 15:24:16
【问题描述】:

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

<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 中尝试过,其他客户端没有。

【问题讨论】:

    标签: php email


    【解决方案1】:

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

    更多信息:http://www.php.net/manual/en/language.types.string.php#language.types.string.syntax.single

    【讨论】:

    • 谢谢。这成功了。我不知道单引号和双引号之间有区别。
    【解决方案2】:
    <?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);
    ?>
    

    【讨论】:

      【解决方案3】:

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

      发送 HTML 消息,使用 &lt;BR&gt; 代替 \n。

      并使用 &lt;PRE&gt;&lt;/PRE&gt; 或 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));
      

      【讨论】:

        猜你喜欢
        • 2016-01-21
        • 1970-01-01
        • 1970-01-01
        • 2011-06-19
        • 2013-07-08
        • 1970-01-01
        • 2018-06-23
        • 2010-11-16
        • 2014-01-21
        相关资源
        最近更新 更多