douju9847
2015-06-19 03:08
浏览 21
已采纳

PHP从textarea进入以发送电子邮件

I created a form for users to key in recipient, subject name, and message to send mail via phpmailer. the problem i'm facing is with the textarea in my form.

I typed this into the textarea:

Hi john,
how's your day?

regards,
your best friend

but right now, this is showing in the email:

Hi john, how's your day? regards, your best friend

any ideas on how to format it to how the user enters in the text area? my current script is just

$body= $_POST["msg"];

i read that i should use nl2br but isnt that for output? Thanks in advance

图片转代码服务由CSDN问答提供 功能建议

我为用户创建了一个表单,用于键入收件人,主题名称和通过phpmailer发送邮件的消息。 我面临的问题是我的表格中的textarea。

我输入了textarea:

 你好约翰,
今天是你的日子吗?
 
 
 
 
 
 
 
 
 
 
遗 n   
 
 

但是现在,这在电子邮件中显示:

 嗨约翰,你今天过得怎么样? 问候,你最好的朋友
   
 
 

关于如何将其格式化为用户如何进入文本区域的任何想法? 我当前的脚本只是

  $ body = $ _POST [“msg”]; 
   
 
 

我读过 我应该使用nl2br,但不是输出? 提前致谢

  • 写回答
  • 好问题 提建议
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • douhu4692 2015-06-19 03:54
    已采纳

    What you will need to do is add a break for each line.

    $text = trim($_POST['textareaname']); // remove the last 
     or whitespace character
    $text = nl2br($text); // insert <br /> before 
     
    

    If you dont like that try this:(you may need to play with it.)

    //trim off excess whitespace off the whole
    $text = trim($_POST['textareaname']);
    
    //explode all separate lines into an array
    $textAr = explode("
    ", $text);
    
    //trim all lines contained in the array.
    $textAr = array_filter($textAr, 'trim');
    
    $str='';
    //loop through the lines
    foreach($textAr as $line){
    $str .= $line."</br> ";
    }
    
    已采纳该答案
    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题