使用甜蜜警报和Ajax发送到邮件。邮件已发送,但未显示成功消息显示错误消息。我该如何解决。
环境:
我试图创建一个简单的联系表 这是前端部分。
function confirm() {
var name = document.getElementById('name').value;
var mail = document.getElementById('email').value;
var too = document.getElementById('too').value;
var from = document.getElementById('from').value;
var passengers = document.getElementById('passengers').value;
var date = document.getElementById('date').value;
var times = document.getElementById('time').value;
var tel = document.getElementById('tel').value;
var message = document.getElementById('messages').value;
var regex = /^([a-zA-Z0-9_.+-])+\@(([a-zA-Z0-9-])+\.)+([a-zA-Z0-9]{2,4})+$/;
var mailTest = regex.test(mail);
var lengths = tel.length;
var waitText = document.getElementById("pulseWarning");
if (name != "" && mail != "" && too != "" && from != "" && passengers != "" && date != "" && times != "" && tel != "") {
if (mailTest) {
if (lengths > 7 && lengths < 15) {
swal({
title: 'Please Wait!',
text: 'Your booking is being processed !',
// timer: 500000,
onOpen: () => {
swal.showLoading()
$.ajax({
url: "/booking",
type: "POST",
data: {
name: name,
email: mail,
to: too,
from: from,
passengers: passengers,
date: date,
time: times,
tel: tel,
message: message
},
dataType: "html",
success: function () {
swal("Done!", "We will contact You !", "success", {
buttons: {
Close: true,
},
})
.then((value) => {
if (value)
location.reload();
});
},
error: function (xhr, ajaxOptions, thrownError) {
swal("Error Subscribing!", "Please try again", "error");
}
});
}
}).then((result) => {
if (
result.dismiss === swal.DismissReason.timer
) {
console.log('I was closed by the timer')
// location.reload();
}
})
}
else {
swal("Invalid Phone Number!", "Please try again", "error");
}
}
else {
swal("Please Enter Valid Email !", "Please try again", "error");
{
}
}
}
else {
swal("Please Fill All Required Field");
}
}
后端部分
app.post("/booking", urlencodedParser, function (req, res, err) {
console.log(req.body);
nodemailer.createTestAccount((err, account) => {
let transporter = nodemailer.createTransport({
service: 'gmail',
secure: false,
port: 25,
secure: false,
auth: {
user: 'mail',
pass: 'password'
},
tls: {
rejectUnauthorized: false
}
});
let mailOptions = {
from: 'name',
to: 'somemail',
subject: 'subject',
text: 'Hello world?',
html: '<b>Hello world?</b>'
};
transporter.sendMail(mailOptions, (error, info) => {
if (error) {
return console.log(error);
}
else {
res.send(true)
console.log('Message sent: %s', info.messageId);
}
});
});
})
仅发送电子邮件成功问题是仅显示错误消息未显示任何成功消息。