我试图在消息中添加文本终于可以了,但是在显示$message
的表单中添加了错误消息之后,添加了null
。
我正在使用ajax进行表单提交。一切正常,但邮件无法正常工作
“邮件”:“邮件错误:邮件正文为空”
<?php
header('Content-Type: application/json');
require("phpmailer/class.phpmailer.php");
$c_name = $_POST['name'];
$c_email = $_POST['email'];
$c_message = $_POST['message'];
$message = '<ul><li>' . $c_name . "</li>";
$message .= '<li>' . $c_email . "</li>";
$message .= '<li>' . $c_message . "</li></ul>";
$mail = new PHPMailer();
$mail->IsSMTP(); // set mailer to use SMTP
$mail->Host = "smtp.gmail.com"; // specify main and backup server
$mail->SMTPAuth = true; // turn on SMTP authentication
$mail->Username = "jswan"; // SMTP username
$mail->Password = "secret"; // SMTP password
$mail->From = "from@example.com";
$mail->FromName = "Mailer";
$mail->AddAddress("josh@example.net", "Josh Adams");
$mail->Port = 587;
$mail->SMTPSecure = 'tls';
$mail->WordWrap = 50; // set word wrap to 50 // add attachments
$mail->IsHTML(true); // set email format to HTML
$mail->Subject = "Enquiry";
$mail->Body = preg_replace('/\[\]/','',$message);
//$mail->Body = $message; //var_dump($message) showing null
$mail->AltBody = $message;
//$mail->AltBody = "This is the body in plain text for non-HTML mail clients";
if (!$mail->send()) {
$result = array('status'=>"error", 'message'=>"Mailer Error: ".$mail->ErrorInfo);//
echo json_encode($result);
} else {
$result = array('status'=>"success", 'message'=>"Message sent.");
echo json_encode($result);
}
exit;
?>
form.js
$(document).ready(function()
{
$('#reused_form').submit(function(e)
{
e.preventDefault();
var formData = $(this).serialize();
debugger;
$.ajax({
type: "POST",
url: 'form/index.php',
dataType: "json",
data: formData,
processData: false,
contentType: false,
cache: false ,
success: function(result) {
console.log(result);
if(result.status=="success"){
$('#success_message').show();
}else if(result.status=="error"){
$('#error_message').show();
}
},
});
});
});