2015-10-15 65 views
0

JavaScript代码转换 n至<br/>仍然打印出<br/>,而不是处理

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代码只是需要从后传来的原始数据,并把它通过电子邮件给我。如上图所示

与身体设置发送

$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但它不处理
,而是将其显示为文字

任何想法我在这里做错了吗?

+0

我也试过nl2br($ contact_comment)在PHP端,它甚至不替换\ n – DerekConlon

+3

是否电子邮件标题有'内容类型:文本/ html'? – Barmar

+3

然后电子邮件标题未设置为text/html .. – Devon

回答

0

集内容类型:您的邮件脚本的标题文字/ 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 <[email protected]>, Kelly <[email protected]>' . "\r\n"; 
$headers .= 'From: Birthday Reminder <[email protected]>' . "\r\n"; 
$headers .= 'Cc: [email protected]' . "\r\n"; 
$headers .= 'Bcc: [email protected]' . "\r\n"; 

mail($to, $subject, $message, $headers);声明

0

之前,如果你想发送邮件中的html内容,那么你必须添加这些头文件。

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