As an application developer, how often we sense the need of 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 pet-project or an app for fun, don't we 😁?
In this article, I am going to 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
You can find the code from my GitHub Repo: Test REST app for Sending e-mails
Nodemailer is a super cool module for 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:
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(,).
let transporter = nodemailer.createTransport({
service: 'gmail',
auth: {
user: '<FROM_EMAIL_ADDRESS>',
pass: '<FROM_EMAIL_PASSWORD>'
}
});
👉 Note: In the above example, the `service` is mentioned as `gmail`. This is just an example. You can specify the name of the e-mail services you want to actually use.
transporter.sendMail(mailOptions, function(error, info){
if (error) {
console.log(error);
} else {
console.log('Email sent: ' + info.response);
}
});
That's all, 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.
node install node-cron --save
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!
Well, this is an optional part for you, if your are not interested in creating a REST API for the purpose of Scheduling and Sending e-mails. In case you 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. Please take a quick look.
'post /api/sendemail': 'EmailController.sendEmail'
Wow, that was quick, isn't it?
👉 Note: With sails.js, you can also use other cron libraries like sails-hook-cron as well. The details can be found here.
Let me know if this was useful to you.
Please feel free to comment on the alternate ways and methods you may be using for serving the same purpose, i.e, Scheduling and Sending e-mails from a node.js ap . After all, Sharing is Caring....!! 😊😊😊