gpt4 book ai didi

javascript - PHP 电子邮件换行符

转载 作者:行者123 更新时间:2023-12-02 16:10:40 25 4
gpt4 key购买 nike

我编写了使用 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 电子邮件,您必须添加正确的 header :

$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);

关于javascript - PHP 电子邮件换行符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30254022/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com