PHP - 表单邮件将换行符转换为空格
我在用户评论表单中有一个
相关的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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
问题是来自文本区域的换行符是
\n
而不是
..因此,在发送邮件之前,请将
\n
替换为
。请记住“\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..Rember user double quoutes in "\n"...
尝试 nl2br() 函数,如果它最初不起作用,请尝试将消息作为 HTML 电子邮件发送。
Try the nl2br() function, if it doesn't initially work try to send the message as an HTML email.