无法将 nodemailer 错误响应从 node.js 发送到 angular.js
Unable to send nodemailer error response from node.js to angular.js
这里我在提交表单时发送一封邮件。一切正常,唯一的问题是即使由于错误的身份验证或互联网问题而未发送邮件,用户也会收到成功消息。如果邮件发送失败,我希望用户收到错误消息。
client
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)
);
});
});
server
function send(req, res) {
console.log(req.body);
var data= req.body
smtpTransport.sendMail({
from: "<email@gmail.com>",
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);
}
您可以将额外的 data.emailSent = true
或 data.emailSent = false
设置并发送为:
if(error){
data.emailSent = false;
} else{
data.emailSent = true;
}
在客户端,您可以检查此标志并相应地显示成功或失败。
服务器:您可以从服务器创建和发送错误消息,如
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
}
感谢 Zeeshan Hassan 和 pooja 帮助我,起初我无法访问状态和消息。我刚刚对 Pooja 的解决方案及其工作方式进行了一些更改
function successCallback(response) {
console.log(response.data.Status);
if (response.data.Status){console.log(response.data.message);}
` `else{ console.log(response.data.message) }
这里我在提交表单时发送一封邮件。一切正常,唯一的问题是即使由于错误的身份验证或互联网问题而未发送邮件,用户也会收到成功消息。如果邮件发送失败,我希望用户收到错误消息。
client
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)
);
});
});
server
function send(req, res) {
console.log(req.body);
var data= req.body
smtpTransport.sendMail({
from: "<email@gmail.com>",
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);
}
您可以将额外的 data.emailSent = true
或 data.emailSent = false
设置并发送为:
if(error){
data.emailSent = false;
} else{
data.emailSent = true;
}
在客户端,您可以检查此标志并相应地显示成功或失败。
服务器:您可以从服务器创建和发送错误消息,如
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
}
感谢 Zeeshan Hassan 和 pooja 帮助我,起初我无法访问状态和消息。我刚刚对 Pooja 的解决方案及其工作方式进行了一些更改
function successCallback(response) {
console.log(response.data.Status);
if (response.data.Status){console.log(response.data.message);}
` `else{ console.log(response.data.message) }