PHP的 - 表格邮件转换线断裂到空格 [英] PHP - Form mail converting line breaks to spaces

查看:114
本文介绍了PHP的 - 表格邮件转换线断裂到空格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在用户注释的表单中有一个< textarea> ,并且当内容传递给表单邮件时,换行符被转换为空格。如何保留表单用户输入的换行符?



相关php:

  $ comments = $ _REQUEST ['comments']; 
//这会从提交的表单中获取注释

// ...

$ to = $ configEmail;

$ subject =收到的网站订单:$ offer;

$ contents =blah blah blah ...;
if(!empty($ comments)){
$ contents = $ contents。\\\
Comments:$ comments\\\
\\\
;
}

// ...

邮件($ to,$ subject,$ contents);

并且在表单的HTML结尾处...(如果它是数据不会丢失)

 < label>评论/问题< / label> 
< textarea name =comments><?php echo $ comments; ?>< / textarea的>

如果我输入:

 第1行
第2行
第3行

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

 第1行第2行第3行$ b  

如何保留换行符?

解决方案

问题是来自textarea的换行符是 \\\
不是< br> ..
因此,在发送前用< br> 替换 \\\
邮件..

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

在\ n中注册用户double quoutes ...

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?

解决方案

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"...

这篇关于PHP的 - 表格邮件转换线断裂到空格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆