我使用HTML和JavaScript (jQuery)编写了从PHP发送电子邮件的代码。在js中,我定义了以下字符串:
var firstT = "It is " + $("#time").html() + " right now.";
var secondT = "And now, it is " + $("#othTime").html() + ".";
var sendT = firstT + "'r'n" + secondT;
然后我将这个字符串发送到PHP文件:
var thTi = "folder/time.php?to=" + $("#perName").val() + "&message=" + sendT;
$.ajax({
url: thTi
});
PHP接收这些字符串,并使用以下代码发送:
<?php
$to = $_GET['to'];
$subject = "The Subject";
$message = $_GET['message'];
$headers = 'From: Me?' . "'r'n" .
'Reply-To: no-reply@thesite.com' . "'r'n" .
'X-Mailer: PHP/' . phpversion();
mail($to, $subject, $message, $headers);
?>
所以,问题是——当一个人收到消息时,它都在一行。我添加了断行'r'n
,甚至尝试了<br>
和<br />
。我发现插入点.
可能会解决问题,但我已经尝试过了,它没有。
要用mail()
发送html电子邮件,您必须添加适当的标题:
$headers = 'MIME-Version: 1.0' . "'r'n";
$headers .= 'Content-type: text/html; charset=utf-8' . "'r'n";
$headers .= 'From: Me?' . "'r'n";
$headers .= 'Reply-To: no-reply@thesite.com' . "'r'n";
$headers .= 'X-Mailer: PHP/' . phpversion();
清理并添加换行符到你的html:
$message = strip_tags($_GET['message'];);
$message_html = preg_replace('/'n/' , '<br />' , $message);
使用双引号"
代替'
,然后'r'n
将工作。所以你的新代码必须看起来像:
$headers = "From: Me?" . "'r'n" .
"Reply-To: no-reply@thesite.com" . "'r'n" .
"X-Mailer: PHP/" . phpversion();
由于您在邮件中使用html页面,您需要做的第一件事是将该邮件的标题类型设置为html。在向$header变量中添加from和其他详细信息之前,将下面的代码行添加到$header变量的开头。
$headers = "MIME-Version: 1.0" . "'r'n";
$headers .= "Content-type:text/html;charset=UTF-8" . "'r'n";
$headers .= 'From: Me?' . "'r'n" .
'Reply-To: no-reply@thesite.com' . "'r'n" .
'X-Mailer: PHP/' . phpversion();
更多的了解这一点,你可以看到这个链接:http://www.w3schools.com/php/func_mail_mail.asp