PHP - Form邮件将换行符转换为空格


PHP - Form mail converting line breaks to spaces

我在表单中有一个用于用户评论的<textarea>,当内容传递给表单邮件时,换行符将被转换为空格。如何保留表单用户键入的换行符?

有关php:

$comments = $_REQUEST['comments'];
// This grabs the comments from the submitted form
//...
$to = $configEmail;
$subject = "Website Order Received: $offer";
$contents = "blah blah blah...";
if (!empty ($comments)) {
    $contents = $contents."'nComments: $comments'n'n";
}
//...
mail($to, $subject, $contents);

在表单的HTML结尾…(如果表单提交错误,则将注释放入表单中,因此数据不会丢失)

<label>Comments / Questions</label>
<textarea name="comments"><?php echo $comments; ?></textarea>

如果我输入:

line 1
line 2
line 3

如果表单提交了错误,它仍然是这样,所以$comments = $_REQUEST['comments'];肯定保留了换行符。但是纯文本电子邮件给了我:

line 1 line 2 line 3

如何保留换行符?

问题是来自文本区的换行符是'n而不是<br> ..因此,在发送邮件之前,将'n替换为<br> .

$body= str_replace("[enter]", "'n",$body);

尝试nl2br()函数,如果它最初不起作用,尝试将消息作为HTML电子邮件发送