简体   繁体   中英

How can send 2 email per one hour using nodemailer in nodejs

  async.each(listofemails, function(to, callback){ mailOptions.to = to; console.log(to); smtpTransport.sendMail(mailOptions, function (err) { if (err) { console.log('Sending to ' + to + ' failed: ' + err); callback(err); } else{ console.log('Sent to ' + to); Company.update({status:'not sent'},{$set:{ status:'sent'}},function(error){ if (error) {console.log("error saving status"+error);} else{console.log("status saved");} }); callback(); } }); 

setInterval(smtpTransport.sendMail,1000*30);

  . ` 

This is my code for sending email. i want to send 10 emails like 2 emails per hour using nodemailer in nodejs

Instead of using javascript, you can use the tools provided by your OS, to execute your node app every hour.

If you're on Linux, you can schedule the execution of your node app with crontab https://www.howtogeek.com/101288/how-to-schedule-tasks-on-linux-an-introduction-to-crontab-files/

If you're on Windows you can use task scheduler : https://technet.microsoft.com/en-us/library/cc748993(v=ws.11).aspx

Create a node script that can send a email on running once.

Then edit crontab by running crontab -e , and append the following line to the file :

0,30 * * * * node /home/ashwani/test/script.js

Now the OS will automatically run script.js every half hour ie twice every hour

In the above code 0 and 30 signify 0th minute and 30th minute of every hour.

To run it every half hour from 10 am till 2:30 pm, you can use following code :

0,30 10,11,12,13,14 * * * node /home/ashwani/test/script.js

For a better understanding, have a loot at https://crontab.guru/#0,30_10,11,12,13,14_ _ _*

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM