0

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: 'dummybot@gmail.com' },
  RowDataPacket { user_email: 'dummybot2@gmail.com' }, ]

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

var mailOptions = {
  from: 'dummybot@gmail.com',
  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.

Young
  • 5
  • 3
  • Following the solution on this URL at the moment but failing... :( https://stackoverflow.com/questions/28527561/sending-email-to-multiple-recipients-via-nodemailer – Young Jan 23 '19 at 21:43
  • emails is stilled defined as 'SELECT user_email FROM scrap' in your code – Tik Jan 23 '19 at 22:35

1 Answers1

1

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: 'dummybot@gmail.com',
  to: to_list,
  subject: 'Sending Email using Node.js[nodemailer]',
  text: 'That was easy!'
};
Tik
  • 822
  • 6
  • 14
  • Thank you for your suggestion I will try it out and let you out if it works! – Young Jan 24 '19 at 09:49
  • So I tried your code perfectly! but I would like to know how to send multiple replies to all mails inside the database (There are multiple emails saved inside the database) - @Tik – Young Jan 24 '19 at 11:41
  • what do you mean by multiple replies? – Tik Jan 24 '19 at 22:41
  • So i figured that if one person send multiple emails, only one email is replied. For example if dummy@a.com ask questio twice, the function will only reply it once not twice. – Young Jan 25 '19 at 03:19