【问题标题】:converting \n to <br/> still prints out the <br/> instead of processing将 \n 转换为 <br/> 仍会打印出 <br/> 而不是处理
【发布时间】:2015-10-15 14:13:47
【问题描述】:

Javascript 代码

var contact_comments = $("#con_us_comment").val();
contact_comments = contact_comments.replace(/(?:\r\n|\r|\n)/g, '<br />');
$.post('post.php', {'con_us_comment':contact_comment}, function(data) {
    // stuff i'm doing with reply from post call
}

PHP 代码只获取来自 post 的原始数据并通过电子邮件发送给我。

$contact_comment = test_input($_POST['con_us_comment']);
$body .= "<br/><b>Comments:  </b> ".$contact_comment;

邮件正文设置如上所示

电子邮件输出如下所示:

Comments:  line 1<br />line 2<br />line 3

代替:

Comments:  line 1
           line 2
           line 3

所以它成功替换了 \n 但它不处理
而是将其显示为文本

知道我在这里做错了什么吗?

【问题讨论】:

  • 邮件标题是否有Content-type: text/html?
  • 那么邮件头没有设置为 text/html..
  • 电子邮件默认为纯文本。如果您要发送 HTML,则需要在标题中说明。
  • manual 中的示例 4
  • 你试过$body .= "&lt;b&gt;Comments: &lt;/b&gt; ".html_entity_decode($contact_comment, ENT_QUOTES, 'UTF-8');吗?

标签: javascript php


【解决方案1】:

在邮件脚本的标题中设置 Content-type: text/html 如果您使用的是 php mail() 函数,请尝试添加

$headers  = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";

// Additional headers
$headers .= 'To: Mary <mary@example.com>, Kelly <kelly@example.com>' . "\r\n";
$headers .= 'From: Birthday Reminder <birthday@example.com>' . "\r\n";
$headers .= 'Cc: birthdayarchive@example.com' . "\r\n";
$headers .= 'Bcc: birthdaycheck@example.com' . "\r\n";

在您的 mail($to, $subject, $message, $headers); 声明之前

【讨论】:

    【解决方案2】:

    如果要在邮件中发送 html 内容,则必须添加这些标头。

    $headers  = 'MIME-Version: 1.0' . "\r\n";
    $headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-12-12
      • 2011-05-21
      • 2012-05-14
      • 2015-01-30
      • 2020-04-16
      • 2012-12-21
      相关资源
      最近更新 更多