ekanna
ekanna

Reputation: 5712

How to Create Email Reminders in Nodejs?

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

Upvotes: 2

Views: 5894

Answers (4)

Ogoh.cyril
Ogoh.cyril

Reputation: 459

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' : '[email protected]'
    },
    {
        'id' : 001,
        'name' : 'user2',
        'dob' : '15-6-2003',
        'email' : '[email protected]'
    },
    {
        'id' : 002,
        'name' : 'user3',
        'dob' : '17-4-2004',
        'email' : '[email protected]'
    },
    {
        'id' : 003,
        'name' : 'user4',
        'dob' : '6-0-1999',
        'email' : '[email protected]'
    }
]

//// 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
                }
            });
        } 
    });
});

Upvotes: 1

abhisekp
abhisekp

Reputation: 5672

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

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

Upvotes: 1

Elf Sternberg
Elf Sternberg

Reputation: 16361

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.

Upvotes: 3

NARKOZ
NARKOZ

Reputation: 27901

You can use node-cron for that.

Upvotes: 2

Related Questions