提交表单后,我在这里发送邮件。一切工作正常,唯一的问题是即使由于错误的身份验证或Internet问题而未发送邮件,用户也收到了成功消息。如果邮件发送失败,我希望用户应收到一条错误消息。
客户
this.mail= function() {
var data = ({
name :this.name,
email :this.email
})
//Post Request
$http({
method: 'POST',
url: '/contact2', data
}).then(function successCallback(response) {
$mdToast.show(
$mdToast.simple()
.textContent('Thanks for your message '+data.name)
.position($scope.getToastPosition())
.hideDelay(5000)
);
}, function errorCallback(response) {
$mdToast.show(
$mdToast.simple()
.textContent('Something went wrong, Please TRY AGAIN '+data.name)
.position($scope.getToastPosition())
.hideDelay(5000)
);
});
});
服务器
function send(req, res) {
console.log(req.body);
var data= req.body
smtpTransport.sendMail({
from: "<[email protected]>",
to: data.email,
subject: "Website Submission from "+data.name,
text: 'You have a new submission with the following details...,
}, function(error, response){ //callback
if(error){
console.log(error);
}if{
console.log(" Message sent "+data.name);
}
smtpTransport.close();
});
res.json(data);
}
服务器:您可以从服务器创建和发送错误消息,如下所示:
if(error){
res.json({
Status : false,
message : error
})
}else{
res.json({ Status : true,
message : 'Success'
})
}
客户:您可以在此处捕获它
function successCallback(response) {
if(response.Status){
console.log(response.message);
}// for success
else{
console.log(response.message)
} //for error
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句