为什么我的PHP邮件脚本给了我一个成功的响应,却没有发送电子邮件



我以前使用过相同的脚本和AJAX查询来处理电子邮件,它们运行良好,但由于某种原因,它在这个网站上不起作用。POST请求表示成功,但另一端没有收到任何电子邮件。

<?php
if ($_SERVER["REQUEST_METHOD"] == "POST") {
# FIX: Replace this email with recipient email
$mail_to = "sample@email.co.nz";

# Sender Data
$fname = str_replace(array("r","n"),array(" "," ") , strip_tags(trim($_POST["fname"])));
$lname = str_replace(array("r","n"),array(" "," ") , strip_tags(trim($_POST["lname"])));
$email = filter_var(trim($_POST["email"]), FILTER_SANITIZE_EMAIL);
$subject = trim($_POST["subject"]);
$message = trim($_POST["message"]);
if ( empty($fname) OR !filter_var($email, FILTER_VALIDATE_EMAIL) OR empty($lname) OR empty($message) OR empty($subject)) {
# Set a 400 (bad request) response code and exit.
http_response_code(400);
echo "Please complete the form and try again.";
exit;
}

# Mail Content
$content = "Name: $fname $lnamen";
$content .= "Email: $emailn";
$content .= "Message:n$messagen";
# email headers.
$headers = "From: $fname $lname";
# Send the email.
$success = mail($mail_to, $subject, $content, $headers);
if ($success) {
# Set a 200 (okay) response code.
http_response_code(200);
echo "Thank you for your message! Someone will be in touch shortly.";
} else {
# Set a 500 (internal server error) response code.
http_response_code(500);
echo "Sorry, something went wrong and we couldn't send your message!";
}
} else {
# Not a POST request, set a 403 (forbidden) response code.
http_response_code(403);
echo "There was a problem with your submission, please try again.";
}
?>

JQuery:

(function($) {
'use strict';
var form = $('#contact_form'),
form_data;
form.submit(function(e) {
e.preventDefault();
form_data = $(this).serialize();
$.ajax({
type: 'POST',
url: form.attr('action'),
data: form_data
})
.done(done_func)
.fail(fail_func);
});
})(jQuery);

inspects的network选项卡显示jquery和PHP都返回良好,并且在其他方面都正常工作。这和一个工作示例之间的唯一区别是实际的HTML内容,但我看不出为什么它在我的HTML 中不起作用

尝试删除标头参数

link to php mail usage 
https://www.w3schools.com/php/func_mail_mail.asp

最新更新