Young
Young

Reputation: 5

Fetch SQL data and send multiple emails with nodemailer?

So I am building this small emailing application to study node.js and mysql.

I have successfully stored data to mySQL database and I stored it like this

var emails = 'SELECT user_email FROM scrap';
con.query(emails, function(err, email, fields){
  //console.log(email);
}); 

The console.log shows

[ RowDataPacket { user_email: '[email protected]' },
  RowDataPacket { user_email: '[email protected]' }, ]

I am currently using nodemailer to send multiple emails at once.

var mailOptions = {
  from: '[email protected]',
  to: emails,
  subject: 'Sending Email using Node.js[nodemailer]',
  text: 'That was easy!'
};

When I execute this command I receive an error

Error: No recipients defined

Is there an alternative way to send multiple emails at once? or is there a way for me to make sure my application send multiple emails accordingly to the emails from database(mySQL). I would like to make sure the app sends reply to all emails stored in the database.

Upvotes: 0

Views: 1249

Answers (1)

Tik
Tik

Reputation: 882

I think you need to get your recipient list into an array first. can be achieved easily in a for loop. be careful not to get confused in your definition of emails and email as well.

var emails = 'SELECT user_email FROM scrap';
var to_list = []
con.query(emails, function(err, email, fields){
  //console.log(email);
  for(k in email){
      to_list.push(email[k].user_email)
    }
}); 

var mailOptions = {
  from: '[email protected]',
  to: to_list,
  subject: 'Sending Email using Node.js[nodemailer]',
  text: 'That was easy!'
};

Upvotes: 1

Related Questions