Arvind kumar
Arvind kumar

Reputation: 87

How to send email after a one week in nodejs

I created a script: when a user signups our app, then I will send a welcome email, then I want after one another will be send.

Note: Now cron job runs every minute, I will change it later.

var job = new CronJob('* * * * *', function() {
    console.log('Cron Job is happen on',Date());
    emailSend()
   
  }, null, true, '');

function emailSend(){
  SingUpDate = something
  comapredate = today-7
  if(comparedate == signupdate){
   sendEmail()
   }
  else{
     return false
   }

}

I want to know about how to stop cron after a week.

Upvotes: 0

Views: 521

Answers (1)

Zac Anger
Zac Anger

Reputation: 7757

You could have your emailSend function remove its own cron job:

var job = new CronJob('* * * * *', function () {
  console.log('Cron Job will happen on', Date())
  emailSend()
}, null, true, '')
// you will need to start the job:
job.start()

function emailSend () {
  SingUpDate = something
  comapredate = today-7
  if (comparedate == signupdate) {
    sendEmail()
    // and then stop it here:
    job.stop()
  } else {
    return false
  }
}

Or you can schedule it for the specific date that you want:

const date = new Date();
date.setDate(date.getDate() + 7)
const job = new CronJob(date, () => {
  console.log(`Scheduled job for ${date} running at ${new Date()}`)
  emailSend()
}, null, true, '')
job.start()

Upvotes: 1

Related Questions