I have a node.js email server that works fine however I noticed a problem. If the query string found 4 rows, it would send four emails but only to the first result instead of to each found email address.
var mysql = require('mysql');
var nodemailer = require('nodemailer');
// Amazon SES is sending the emails - uses this module for node
var ses = require('nodemailer-ses-transport');
//transport for connecting with Amazon SES
var transporter = nodemailer.createTransport(ses({
accessKeyId: '****************',
SecretAccessKey: '*************'
}));
// Mysql Connection to database
var connection = mysql.createConnection({
*****connection info goes here *****
});
//Connect to mysql database and check for errors
connection.connect(function(err) {
if (err) {
console.error('error connecting: ' + err.stack);
return;}
});
var querystring1 = 'SELECT `date_created`,`first_name`,`last_name`,`email` FROM `users` WHERE TIMESTAMPDIFF(SECOND,date_created, NOW()) <=298 AND `email`!="" LIMIT 0,5';
connection.query(querystring1, function (err, row) {
if (err) throw err;
for (var i in row) {
// Email content; change text to html to have html emails. row[i].column name will pull relevant database info
var sendit = {
to: row[i].email,
from: '******@******',
subject: 'Hi ' + row[i].first_name + ', Thanks for joining ******!',
html: {path:__dirname + '/templates/welcome.html'}
};
// Send emails
transporter.sendMail(sendit,function(error,response){
if (error) {
console.log(error);
}else{
console.log("Message sent1: " + row[i].first_name);}
transporter.close();}
)
}});
....
How do I have it loop through each found row and send a custom email using row data individual row data?
Do not close the transporter until all your emails are sent.
var querystring1 = 'SELECT `date_created`,`first_name`,`last_name`,`email` FROM `users` WHERE TIMESTAMPDIFF(SECOND,date_created, NOW()) <=298 AND `email`!="" LIMIT 0,5';
connection.query(querystring1, function (err, row) { if (err) throw err;
var toSend=[];
for (var i in row) { // Email content; change text to html to have html emails.
toSend.push({//row[i].column name will pull relevant database info
to: row[i].email,
from: '******@******',
subject: 'Hi ' + row[i].first_name + ', Thanks for joining ******!',
html: {path:__dirname + '/templates/welcome.html'}
});
}
// Send emails
async.eachSeries(toSend, function( sendit, callback) {
transporter.sendMail(sendit,function(error,response){
if (error) {
callback(error);
}else{
console.log("Message sent1: " + sendit.to); callback();
}
)},function(err){if (err) throw (err); console.log("Finished")};
});
});
try this
// Send emails
(function (mail) {
transporter.sendMail(mail,function(error,response){
if (error) {
console.log(error);
}else{
console.log("Message sent1: " + mail.first_name);}
transporter.close();
}
)
)(sendit);
Please use this one, I have tested and it works fine:
function sendmail(sendit, username){
transporter.sendMail(sendit,function(error,response){
if (error) {
console.log(error);
} else {
console.log("Message sent: " + username);}
transporter.close();
}
)
};
for (var i in row) {
var sendit = {
to: row[i].email,
from: 'sandeepv@gmail.com',
subject: 'Hi ' + row[i].first_name + ', Thanks for joining!',
html: {path:__dirname + '/welcome.html'},
};
// Send email
sendmail(sendit, row[i].first_name);
}