我的VPS服务器在发送邮件时遇到了一个问题,我的服务器安装了PHP和sendmail。
我试着从一个HTML表格中发送邮件,这个表格在JS中检查,然后用HTML发送,请求启动得很好,但没有发送任何东西。
我的JS代码 。
submitMail() {
const emailValue = document.getElementById('mail').value;
const subjectValue = document.getElementById('subject').value;
const messageValue = document.getElementById('text').value;
const xhr = new XMLHttpRequest();
xhr.open('POST', 'https://ag-dev.fr/mailform.php', true);
xhr.setRequestHeader('Content-Type', 'application/json');
xhr.onreadystatechange = () => {
if (xhr.readyState === 4 && xhr.status === 200) {
this.sendComplet();
}
};
xhr.send(JSON.stringify({
email: emailValue,
subject: subjectValue,
message: messageValue,
}));
},
我的PHP代码。
<?php
if (!empty($_POST['message']))
{
$to = '[email protected]';
$from = $_POST['email'];
$subject = $_POST['subject'];
$message = $_POST['message'] . "\n\n\n\n Ceci est un message envoyé depuis le formulaire de contact. Pour répondre à ce mail, envoyez un mail à l'adresse suivante : " . $_POST['email'];
$headers = 'MIME-Version: 1.0\r\n';
$headers .= 'Content-type: text/html; charset=iso-8859-1\r\n';
$headers .= 'To: Guyomar Alexis <[email protected]>\r\n';
$headers .= 'From: ' . ' <' . $from . '>\r\n';
mail($to,$subject,$message,$headers);
}
?>
我在我的命令行服务器上用sendmail运行测试,我收到了邮件,没有问题。我想是我的代码或我的配置服务器出了问题。
如果有人有任何想法,这可能是从哪里来的... ...
谢谢你@ADyson的JSON POST与PHP链接.我解决我的问题,像这样。
<?php
$data = json_decode(file_get_contents('php://input'), true);
if (!empty($data))
{
$to = '[email protected]';
$from = $data['email'];
$subject = utf8_decode($data['subject']);
$message = utf8_decode($data['message']) . utf8_decode("\n\n\n\n Ceci est un message envoyé depuis le formulaire de contact. Pour répondre à ce mail, envoyez un mail à l'adresse suivante : " . $from);
$headers = 'MIME-Version: 1.0\r\n';
$headers .= 'Content-type: text/html; charset=utf-8\r\n';
$headers .= 'To: Guyomar Alexis <[email protected]>\r\n';
$headers .= 'From: ' . ' <' . $from . '>\r\n';
mail($to,$subject,$message,$headers);
}
?>
现在所有的运行良好。