I try to make seperate function for nodemailer and expecting boolean true or false in return.But when the mail is send to user I'm getting the mail in account but after that instead of true or false in return I'm getting value as undefined.
function nodeMailer(mailOptions){
var transporter = nodemailer.createTransport({
service: 'gmail',
auth: {
user: get.username,
pass: get.password
},
tls: {
rejectUnauthorized : false
}
});
transporter.sendMail(mailOptions, (error, info) => {
if(error) return false;
return true;
});
}
You are getting undefined, because true or false should return your nodeMailer function and not transporter.sendMail.
In this case you can create Promise which can help you with that.
var nodeMailer = function(mailOptions) {
return new Promise(function(resolve, reject){
var transporter = nodemailer.createTransport({
service: 'gmail',
auth: {
user: get.username,
pass: get.password
},
tls: {
rejectUnauthorized : false
}
});
transporter.sendMail(mailOptions, (error, info) => {
if(error) reject(error);
resolve(info);
});
}
});
}
and then call your promise like below
nodeMailer(yourOptions).then(function(result) {
// handle success result here
return info;
})
.catch(function(err) {
// handle your error here
})
more about promises you can read here. They are very useful when you need to control your async flow.
Try this by using callback or promise.