2

I have a simple form containing 4 fields viz:

  1. Name
  2. DateOfBirth
  3. email Address
  4. Message

I save this data to mongodb. On birthday, i need to send a email reminder. I use node_mailer for sending mails. But how to set up the reminders to send mails on specific date? I am running nodejs server.

Thanks

MPelletier
  • 16,256
  • 15
  • 86
  • 137
ekanna
  • 5,462
  • 8
  • 28
  • 31

4 Answers4

3

Don't use node to track dates like that. Don't re-invent wheels.

Your platform, being it Mac, Linux, or Windows, has a scheduler on it. The traditional one is called 'cron'. Use that to start a simple wrapper to node_mailer that will scan the database for "today's birthdays" that will send the emails instead.

jcolebrand
  • 15,889
  • 12
  • 75
  • 121
Elf Sternberg
  • 16,129
  • 6
  • 60
  • 68
  • Just curious: Can you name an OS (other than an embedded one, in which case the application scenario described would be absurd) that doesn't have a scheduler of some kind, one that can start an arbitrary process? – Elf Sternberg Aug 24 '11 at 17:38
  • There is an issue. How to track if the email has been sent successfully before? If the cronjob is run multiple times per day to send reminders, then how to handle this case? – abhisekp Apr 16 '19 at 13:49
  • @abhisekp you can simply create a table in your DB to track if the emails sent. Query that with a Join to find whether you need to send an email. After sent mark it in the DB. You can reset as per your application logic – kasvith Sep 15 '20 at 08:37
2

You can use node-cron for that.

NARKOZ
  • 27,203
  • 7
  • 68
  • 90
1

It's Just Basic Looping That Is Required

You Loop through the user everyday at a particular time then check if the day and month matches and shoot your mail

**Here A Sample code Below ** cheers

const cron = require('node-cron');
const mailer = require('nodemailer');

//T0 Get the Current Year, Month And Day
var dateYear = new Date().getFullYear();
var dateMonth = new Date().getMonth(); // start counting from 0
var dateDay = new Date().getDate();// start counting from 1

/* The Schema Which The Database Follow 
    {
        'id' : number,
        'name' : string,
        'dob' : string (day - month),
        'email' : string 
    },
 * You can Use Any type of schema (This is the method I preferred)
*/

/// database goes here 
var users = [
    {
        'id' : 000,
        'name' : 'user1',
        'dob' : '14-6-1994',
        'email' : 'user1@exapmle.com'
    },
    {
        'id' : 001,
        'name' : 'user2',
        'dob' : '15-6-2003',
        'email' : 'user2@exapmle.com'
    },
    {
        'id' : 002,
        'name' : 'user3',
        'dob' : '17-4-2004',
        'email' : 'user3@exapmle.com'
    },
    {
        'id' : 003,
        'name' : 'user4',
        'dob' : '6-0-1999',
        'email' : 'user4@exapmle.com'
    }
]

//// credentials for your Mail
var transporter = mailer.createTransport({
    host: 'YOUR STMP SERVER',
    port: 465,
    secure: true,
    auth: {
        user: 'YOUR EMAIL',
        pass: 'YOUR PASSWORD'
    }
});
//Cron Job to run around 7am Server Time 
cron.schedule('* * 07 * * *', () => {
    ///The Main Function 
    const sendWishes =  
    // looping through the users
    users.forEach(element => {
        // Spliting the Date of Birth (DOB) 
        // to get the Month And Day
        let d = element.dob.split('-')
        let dM = +d[1]  // For the month
        let dD = +d[0] // for the day 
        let age = dateYear - +d[2]
        console.log( typeof dM) //return number
        // Sending the Mail
        if(dateDay == dD && dateMonth == dM ){
            const mailOptions = {
                from: 'YOUR EMAIL',
                to: element.email,
                subject: `Happy Birthday `,
                html: `Wishing You a <b>Happy birthday ${element.name}</b> On Your ${age}, Enjoy your day \n <small>this is auto generated</small>`                       
            };
            return transporter.sendMail(mailOptions, (error, data) => {
                if (error) {
                    console.log(error)
                    return
                }
            });
        } 
    });
});
Ogoh.cyril
  • 462
  • 7
  • 14
0

I found agendajs highly reliable and better with the GUI complement agendash

Resource for Getting Started: NodeJS: scheduling tasks with Agenda.js

abhisekp
  • 4,648
  • 2
  • 30
  • 37