Send and Schedule e-mails from a Node.js app

Co-Founder, @CreoWis | Teacher, @tapaScript | Founder, @ReactPlay | YouTuber | Writer | Human
Search for a command to run...

Co-Founder, @CreoWis | Teacher, @tapaScript | Founder, @ReactPlay | YouTuber | Writer | Human
Sir, I am building an REST api in node.js.It store a meeting time and 30 before that time it's send a notifications for that particular meeting.How can I trigger this function for a particular time of every meeting.
Hello Diptom Saha,
You can use node-cron for creating a cron job if it has to be happening at a particular time of the day, day of the week, and so on.
let cron = require('node-cron');
In your case, as the meeting time is known, the cron job can be created with the time that is meeting_time - time_offset(30 mins for example).
The cron.schedule method is capable of taking the customizable pattern too.
cron.schedule('* * * * *', () => {
console.log('running a task every minute');
});
Cron allowed fields are,
So when you schedule the meeting, schedule a cron also accordingly.
Hope it helps.
Traditional SSR is great, but it has a massive buffering problem. Let's look into the SSR Streaming and learn how it could be a game changer.

Learn how to build maintainable, reusable forms in React using design patterns like custom hooks, compound components, and context for scalable CRUD

Learn how JavaScript variables work, including let vs const, primitive and reference types, pass by value, and how JavaScript stores data in memory.

A beginner-friendly introduction to JavaScript covering what it is, how it works, and how to set up your JavaScript environment

Learn how the promises are handled internally using event loop. This concept is essentials to debug and work with async programming.

GreenRoots Blog - A Blog by Tapas Adhikary
186 posts
Educator | YouTuber | Building ReactPlay | Let's Connect
As an application developer, how often do we sense the need for a service that would send e-mails to specified or subscribed email ids? Even if there is no real need, we still fantasize about it while developing a side-project or an app for fun, don't we π?
In this article, I will explain the simple steps to send emails from your node.js app. At the end of it, we will be able to schedule and send e-mails. Not only that, we will be creating a REST API to post required details to a node.js app for sending emails.
TL;DR
Here is the working app
REST API to send e-mail from a node.js app
You can find the code from my GitHub Repo: Test REST app for Sending e-mails
Nodemailer is a super cool module for the Node.js application to allow email sending so easily. Install it using npm
npm install nodemailer --save
Import Nodemailer in your .js(app.js, email.js whatever) file:
let nodemailer = require('nodemailer');
Next, follow these three simple steps to get things working:
Setup a message option: This is to tell Nodemailer who is sending what message to whom?
let mailOptions = {
from: '<FROM_EMAIL_ADDRESS>',
to: '<TO_EMAIL_ADDRESS>',
subject: 'Email from Node-App: A Test Message!',
text: 'Some content to send'
};
π Note: The to property above can have multiple email ids separated by commas(,).
Create a Nodemailer transporter using either SMTP(this is default) or some other transport mechanism
let transporter = nodemailer.createTransport({
service: 'gmail',
auth: {
user: '<FROM_EMAIL_ADDRESS>',
pass: '<FROM_EMAIL_PASSWORD>'
}
});
π Note: In the above example, we have mentioned the service as gmail. It is just an example. You can specify the name of the e-mail services you want to use.
Use the sendMail() method of your previously created transporter to deliver the message.
transporter.sendMail(mailOptions, function(error, info){
if (error) {
console.log(error);
} else {
console.log('Email sent: ' + info.response);
}
});
That's all, and you are done. Now we have everything required to send e-mail from this node.js app.
Yeah, right! The real power of this app comes with the fact that you will be able to schedule the emails like,
You guessed it right, we need something like a cron job, and for that, we will be using a node module called node-cron.
First install it.
node install node-cron --save
Import node-cron and schedule a task
let cron = require('node-cron');
cron.schedule('* * * * *', () => {
console.log('running a task every minute');
});
π Note: You can read about several cron schedule patterns here. In the above example, we have scheduled a simple console log in every minute.
Here is the combined code where I am scheduling the e-mail to send every minute:
let cron = require('node-cron');
let nodemailer = require('nodemailer');
// e-mail message options
let mailOptions = {
from: '<FROM_EMAIL_ADDRESS>',
to: '<TO_EMAIL_ADDRESS>',
subject: 'Email from Node-App: A Test Message!',
text: 'Some content to send'
};
// e-mail transport configuration
let transporter = nodemailer.createTransport({
service: 'gmail',
auth: {
user: '<FROM_EMAIL_ADDRESS>',
pass: '<FROM_EMAIL_PASSWORD>'
}
});
cron.schedule('* * * * *', () => {
// Send e-mail
transporter.sendMail(mailOptions, function(error, info){
if (error) {
console.log(error);
} else {
console.log('Email sent: ' + info.response);
}
});
});
TADAAA, Done!

It is an optional part for you if you are not interested in creating a REST API for Scheduling and Sending e-mails. In case your mind, we can do it in a minute using sails.js. I have posted an article on How to Deploy your Sails.js app on Heroku and live longer. Take a quick look.
routes.js file of your sails.js app'post /api/sendemail': 'EmailController.sendEmail'
EmailController with the sendEmail method. This method should have the code discussed above for scheduling and sending emails.Wow, that was quick.
π Note: With sails.js, you can also use other cron libraries like sails-hook-cron as well. You can find the details here.
Let me know if this was useful to you.
Please feel free to comment on the alternate ways and methods you may use to serve the same purpose, i.e., Scheduling and Sending e-mails from a node.js app. After all, Sharing is Caring....!! πππ
I hope you enjoyed this article or found it helpful. Let's connect. You can find me on Twitter(@tapasadhikary), sharing thoughts, tips, and code practices. Please hit the Subscribe button at the top of the page to get an email notification on my latest posts.