Reputation: 447
I'm working on my first express project. I'm using Nodemailer and it's sending emails just fine but I'm not getting a response. Any insights? Here is the function I'm using to send the emails. It console logs message sent just fine, but doesn't seem to respond.
exports.email = function(req, res, options, tokens) {
receiver_id= options.email.receiver_id;
message= options.email.message;
sender_email = options.email.sender_email;
sender_name = options.email.sender_name;
slug=options.info.slug;
org_name=options.info.org_name;
dir_url=options.info.dir_url;
var https = require('https');
str = '';
//getting user email by id
path = '/api/v1/people/' + receiver_id +'?__proto__=&access_token=' + tokens[slug];
var options = {
host: slug + '.nationbuilder.com',
path: path,
method: "GET",
json: true,
headers: {
"content-type": "application/json",
"accept": "application/json"
},
}
var nb_req = https.get(options, req_callback);
function req_callback(response, res) {
response.on('data', function(chunk) {
str += chunk;
});
response.on('end', function() {
object = JSON.parse(str);
receiver_email = object.person.email1;
var nodemailer = require('nodemailer');
var transporter = nodemailer.createTransport({
port:465,
host:"smtp.gmail.com",
auth: {
user: '[email protected]', // Your email id
pass: '69$6FK$b6PQu' // Your password
}
});
var html = '<p>From: ' + sender_email + '<br>';
html += 'To: ' + receiver_email + '</p>';
html += "<p>"+message + "</p>"
html += "<p>This message was delivered by the " + org_name + " alumni directory website, " + "<a href='" + dir_url
+ "'>" + dir_url + "</p>";
var mailOptions = {
from: '[email protected]', // sender address
to: '[email protected]', // list of receivers
subject: "A message from " + sender_name, // Subject line
/*text: "Message sent to", // plaintext body*/
html: html
};
transporter.sendMail(mailOptions, function(error, info){
if(error){
console.log(error);
res.json({yo: 'error'});
}else{
console.log('Message sent: ' + info.response);
res.sendStatus(200);
};
return res.sendStatus(200);
});
});
}
}
Upvotes: 1
Views: 5366
Reputation: 1
use res.status(200).json() instead:
transporter.sendMail(mailOptions, function(error, info){
if (error){
console.log(error);
res.json({yo: 'error'});
res.sendStatus(500);
}else{
console.log('Message sent: ' + info.response);
res.status(200).json({"msg": "mesage has been sent"})
};});
Upvotes: 0
Reputation: 508
This is the asynchronous problem of JavaScript. I also have the same problem. When you call the email function outside it will not return anything so you will get 'undefined', because you returned a response within a callback function.
router.post('/"your api path"',async(req,res)=>{
//remaining codes
transporter.sendMail(mailOptions, function(error, info){
if(error){
console.log(error);
res.status(400).json({yo: 'error'});
}else{
console.log('Message sent: ' + info.response);
res.sendStatus(200);
};
return res.sendStatus(200);
});
}
Upvotes: 0
Reputation: 319
The following code worked for me in the transporter verify and sendMail functions.
transporter.verify(function (error, success) {
if (error) {
console.log(error);
} else {
console.log("Server is ready to take our messages");
res.send(success);
res.status(200);
}
});
transporter.sendMail(mailData, function (err, info) {
if (err) {
console.log("error: ", err);
} else {
console.log("all good", info);
res.sendStatus(200);
}
return res.status(200).end();
});
}
Upvotes: 0
Reputation: 19945
You are missing res.end()
after res.sendStatus(200);
.
transporter.sendMail(mailOptions, function(error, info){
if (error){
console.log(error);
res.json({yo: 'error'});
res.sendStatus(500);
}else{
console.log('Message sent: ' + info.response);
res.sendStatus(200);
};
return res.end();
}) ;
Upvotes: 2