node.js: looping, email each matching row from query but it it emails the same user by # number of matches?
Posted
by
udonsoup16
on Stack Overflow
See other posts from Stack Overflow
or by udonsoup16
Published on 2014-08-20T19:05:28Z
Indexed on
2014/08/21
22:20 UTC
Read the original article
Hit count: 157
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 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?
© Stack Overflow or respective owner