折腾:
期间,修改了html页面,也同步更新了js和php代码后,去上传到自己的wordpress网站中的某个子目录下面去看看效果。
结果提交表单,内部js调用php的mail去发送邮件,结果失败
相关代码:
js:
function submitForm(){
// Initiate Variables With Form Content
var name = $("#name").val();
var email = $("#email").val();
// var msg_subject = $("#msg_subject").val();
var message = $("#message").val();
$.ajax({
type: "POST",
url: "php/form-process.php",
// data: "name=" + name + "&email=" + email + "&msg_subject=" + msg_subject + "&message=" + message,
data: "name=" + name + "&email=" + email + "&message=" + message,
success : function(text){
if (text == "success"){
formSuccess();
} else {
formError();
submitMSG(false,text);
}
}
});
}
和:
php:
<?php
$errorMSG = "";
// NAME
if (empty($_POST["name"])) {
$errorMSG = "Name is required ";
} else {
$name = $_POST["name"];
}
if (empty($_POST["email"])) {
$errorMSG .= "Email is required ";
} else {
$email = $_POST["email"];
}
// // MSG SUBJECT
// if (empty($_POST["msg_subject"])) {
// $errorMSG .= "Subject is required ";
// } else {
// $msg_subject = $_POST["msg_subject"];
// }
// MESSAGE
if (empty($_POST["message"])) {
$errorMSG .= "Message is required ";
} else {
$message = $_POST["message"];
}
// $EmailTo = "xxx";
// $Subject = "New Message Received";
// $EmailTo = "xxx";
$EmailTo = "xxx";
$Subject = $name . " " . $email . " " . "希望联系睿小麟";
echo "errorMSG:" . $errorMSG . " Subject:" . $Subject;
// prepare email body text
$Body = "";
$Body .= "Name: ";
$Body .= $name;
$Body .= "\n";
$Body .= "Email: ";
$Body .= $email;
$Body .= "\n";
$Body .= "Subject: ";
// $Body .= $msg_subject;
$Body .= $Subject;
$Body .= "\n";
$Body .= "Message: ";
$Body .= $message;
$Body .= "\n";
// send email
$success = mail($EmailTo, $Subject, $Body, "From:".$email);
// redirect to success page
if ($success && $errorMSG == ""){
echo "success";
}else{
if($errorMSG == ""){
echo "Something went wrong :(";
} else {
echo $errorMSG;
}
}
?>
结果失败:
php call mail error
php – How can I get the error message for the mail() function? – Stack Overflow
如果出错了,调用 error_get_last()去看看具体是什么错
// send email
$success = mail($EmailTo, $Subject, $Body, "From:".$email);
// redirect to success page
if ($success && $errorMSG == ""){
echo "success";
}else{
if($errorMSG == ""){
$mailError = error_get_last()[‘message’];
echo "Send mail fail: " . $mailError;
} else {
echo $errorMSG;
}
}
结果是:
空的
用了:
// $mailError = error_get_last()[‘message’];
// echo "Send mail fail: " . $mailError;
print_r(error_get_last());
也是空的。
[RESOLVED] Unknown error in PHP’s mail() function on Project:Support desk
Fatal error: Call to undefined function: mail()
php send email
PHPMailer/PHPMailer: The classic email sending library for PHP
有空去试试这个PHPMailer
How to send an email using PHP? – Stack Overflow
-》但是要清楚的是:
mail只是提交发送邮件的请求,是否成功
首先要确保 请求发送邮件 提交成功了
更主要的是保证:当前host服务器中支持SMTP邮件,本地服务器已经配置好了SMTP-》这样才支持发送邮件
-〉可能是自己的主机中,暂时没有配置好发送邮件的服务,所以邮件才发送不成功?
email – php mail setup in xampp – Stack Overflow
转载请注明:在路上 » 【未解决】html页面中js调用php的mail去发送邮件失败