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

发布于 2024-12-24 19:16:19 字数 916 浏览 1 评论 0原文

我在用户评论表单中有一个

相关的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

如何保留换行符?

I have a <textarea> in a form for user comments, and when the contents are passed to form mail, the line breaks are being converted to spaces. How can I preserve the line breaks that the form's user types in?

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

And in the HTML end of the form... (the comments are put into the form if it's submitted with errors, so data isn't lost)

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

If I type:

line 1
line 2
line 3

It remains like that if the form is submitted with errors, so $comments = $_REQUEST['comments']; is definitely preserving the line breaks. But the plain-text e-mail gives me:

line 1 line 2 line 3

How can I preserve the line breaks?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

梦亿 2024-12-31 19:16:19

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

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

请记住“\n”中的用户双引号...

The problem is the line breaks coming from the textarea are \n not <br>..
So replace the \n by <br> before sending the mail..

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

Rember user double quoutes in "\n"...

夏天碎花小短裙 2024-12-31 19:16:19

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

Try the nl2br() function, if it doesn't initially work try to send the message as an HTML email.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文