【问题标题】:PHP getting enter from textarea to send emailPHP从textarea输入以发送电子邮件
【发布时间】:2015-06-19 03:08:03
【问题描述】:

我创建了一个表单供用户键入收件人、主题名称和消息以通过 phpmailer 发送邮件。我面临的问题是我表单中的文本区域。

我在 textarea 中输入了这个:

Hi john,
how's your day?

regards,
your best friend

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

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

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

$body= $_POST["msg"];

我读到我应该使用 nl2br,但这不是用于输出吗?提前致谢

【问题讨论】:

  • 我认为您需要打开 html 电子邮件,以便它能够识别换行符
  • 我正在使用 phpmailer,我有 $mail->isHTML(true);放。你说的是这个吗?
  • “我读到我应该使用 nl2br 但不是输出吗?”老兄,你测试了它 - 它有效吗?
  • 我测试过了。它不起作用。我想我可能需要一些东西来感知输入并给它一个
    。目前,如果我手动输入任何 html,它可以在电子邮件中使用。有什么想法吗?

标签: php html


【解决方案1】:

您需要为每一行添加一个换行符。

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

如果你不喜欢,试试这个:(你可能需要玩它。)

//trim off excess whitespace off the whole
$text = trim($_POST['textareaname']);

//explode all separate lines into an array
$textAr = explode("\n", $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> ";
}

【讨论】:

  • 顺便说一句,如果我想更进一步做一些类似 stackoverflow 中的 textarea 的事情,请问我应该从哪里开始?
  • 您的意思是向文本区域添加代码格式?发布另一个问题并在此处发布链接。
  • 用我用的回答。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-04-22
  • 2011-03-04
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多