Beginner’s Guide On Sending Automated Emails With Node.js, Nodemailer, and Cron Jobs
Tags: node, nodemailer, javascript, webdev
Published: 2023-10-24 17:12:09

<p data-pm-slice="1 5 []">Sending scheduled using Nodemailer, automated emails is a common need for many web apps and services. For example, you may want to send weekly digests to users, automatic reminders or notifications at set times, deliver scheduled reports, and so on.</p>
In this comprehensive tutorial, I will teach how to use Node.js, Nodemailer, and cron jobs to send automated emails on a recurring schedule. We’ll walk through a step-by-step process for setting up everything you need, including:
<ul>
 	<li><strong>Nodemailer for sending emails from Node.js</strong></li>
 	<li><strong>HTML email templates and styles</strong></li>
 	<li><strong>A Bull queue for processing email jobs</strong></li>
 	<li><strong>Cron jobs to schedule adding emails to the queue</strong></li>
 	<li><strong>Handling errors and logging</strong></li>
</ul>
By the end, you’ll know how to integrate scheduled email sending into your Node.js apps!

Are you ready?
<h4>Prerequisites</h4>
This tutorial assumes:
<ul>
 	<li>Basic knowledge of JavaScript and Node.js</li>
 	<li>Experience working with a command line/terminal</li>
 	<li><a class="attrlink" href="https://nodejs.org/en/download" target="_blank" rel="noopener noreferrer nofollow">Node.js installed</a> on your development machine</li>
 	<li><a class="attrlink" href="https://phoenixnap.com/kb/windows-smtp-server" target="_blank" rel="noopener noreferrer nofollow">Access to an SMTP service</a> for sending emails</li>
</ul>
We’ll use <a class="attrlink" href="https://blog.learnhub.africa/2023/01/18/how-to-send-an-email-in-node-js-using-nodemailer/" target="_blank" rel="noopener noreferrer nofollow">Nodemailer to send emails,</a> Bull for queues, and node-cron for cron jobs. You’ll also need access to a MongoDB or similar database to store logs.
<h4>Setting Up Nodemailer</h4>
First, we need to set up Nodemailer, which will handle sending the actual emails from our Node.js application.

Install Nodemailer via npm:

<code>npm install nodemailer</code>

Then, require it in your app:

<code>const nodemailer = require('nodemailer');</code>

Next, we must create a transporter object from nodemailer configured to use our chosen email-sending service.

For example, if using Mailgun, it would look like this:
```javascript
const transporter = nodemailer.createTransport({
  service: 'Mailgun',
  auth: {
    user: process.env.MAILGUN_USER,
    pass: process.env.MAILGUN_PASS  
  }
});
```
Make sure to store your service credentials securely in environment variables.

You can configure Nodemailer for any SMTP service, including SendGrid, Mailjet, Amazon SES, Gmail, and more. Just refer to the respective Nodemailer transport setup docs for any service.

Optionally, you may also want to create and use email templates for the body content of the emails you send. Handlebars templates work great for this.

First, install the nodemailer-express-handlebars package:

<code>npm install nodemailer-express-handlebars</code>

Then you can configure Nodemailer to use it:
```javascript
const handlebarsOptions = {
  viewEngine: {
    extName: '.html',
    partialsDir: path.resolve('./emails'),
    defaultLayout: false,
  },
  viewPath: path.resolve('./emails'),
  extName: '.html',
};
transporter.use('compile', hbs(handlebarsOptions));
```
This allows you to create .html email templates that will be used to generate the email body content.

With Nodemailer configured, we’re ready to start creating email templates.
<h4>Creating the Email Templates</h4>
For maximum reusability and consistency, you’ll want to create HTML email templates that can be used to generate the content for scheduled emails.

Start by creating an emails directory where the templates will live:

<code>mkdir emails</code>

Inside emails, you can create templates like:
<ul>
 	<li>digest.html - for a weekly digest</li>
 	<li>notification.html - for notifications</li>
 	<li>report.html - for reports</li>
</ul>
And so on, depending on your needs.

The templates should contain your emails' basic HTML structure and any placeholders for dynamic data like {firstName}, {digestReports} etc.

For example:
```xml

html>
  <body>
    <p>Hi {firstName}, here's your weekly digest:</p>

    {{{digestReports}}}

    <p>Let us know if you have any questions!</p>
  </body>
</html>
```
The templates can also include CSS styling for layout and formatting:
```xml
<style>
  /* CSS rules */
</style>
```
This allows you to create reusable templates that can be rendered with different data to generate the full emails.
<h4>Building the Email Queue</h4>
We'll use a queue system to process sending the scheduled emails efficiently. This allows concurrently processing jobs without getting rate-limited or overwhelmed.

We’ll use the bull package for implementing a queue in our app.

Install Bull:

<code>npm install bull</code>

Then, create a Queue instance:
```javascript
const Queue = require('bull');
const emailQueue = new Queue('email queue', {
  redis: {
    host: '127.0.0.1',
    port: 6379,
  }
});
```
This creates a emailQueue that Redis will go back to store the jobs.

To add a job to the queue, call:
```javascript
emailQueue.add({
  // job data
});
```
The job object can contain any data needed to process the email, like recipients, templates, variables, etc.

Bull will then process these jobs in the background. To start processing, call:
```javascript
emailQueue.process(async (job) =&gt; {
  // send email
});
```
This processes jobs sequentially by default. You can handle concurrency like:
```javascript
emailQueue.process(5, async (job) =&gt; {
  // send email 
}); 
```
This will process 5 jobs at once. Tweak based on your app’s needs and volume.

Now, add it to the queue whenever you need to send an email! The queue will make sure it gets sent smoothly.
<h4>Configuring Cron Jobs</h4>
To schedule running jobs that add emails to our queue, we’ll use cron jobs.

Cron allows scheduling tasks using time-based syntax like:
```javascript
* * * * *

┬ ┬ ┬ ┬ ┬

│ │ │ │  │

│ │ │ │ │

│ │ │ │ │

│ │ │ │ └───── day of week (0 - 7) (0 to 6 are Sunday to Saturday, or use names)

│ │ │ └────────── month (1 - 12)

│ │ └─────────────── day of month (1 - 31)

│ └──────────────────── hour (0 - 23)

└───────────────────────── min (0 - 59)
```
So 0 0 * * * would run at midnight every day.

We’ll use the node-cron package to create cron-powered tasks in JavaScript.

Install it:

<code>npm install node-cron</code>

Then you can create a cron task like:
```javascript
const cron = require('node-cron');
cron.schedule('0 0 * * *', () =&gt; {
  // run at midnight every day
});
```
For our email scheduler, we likely want to create a few different cron tasks for different intervals like:
```javascript
// Daily digest at 9am

cron.schedule('0 9 * * *', () =&gt; {

  addDailyDigestEmail(); 

});


// Weekly reports each Monday

cron.schedule('0 9 * * 1', () =&gt; {

  addWeeklyReportEmail();

}); 


// Monthly newsletter on 1st of month

cron.schedule('0 9 1 * *', () =&gt; {

  addMonthlyNewsletter();

});
```
This allows us to schedule repeating email jobs easily!
<h4>Sending the Emails</h4>
With our queue processing setup, templates defined, and cron jobs scheduled - we're ready to handle email sending.

In each cron job, we need to add a job to the queue with the necessary data:
```javascript
// Add daily digest 

cron.schedule('0 9 * * *', () =&gt; {
  const data = {
    template: 'digest',
    recipients: ['user1@email.com', 'user2@email.com'],
  };
  emailQueue.add(data);
});
```
The queue processor will pick this up, with concurrency defined:
```javascript
emailQueue.process(5, async (job) =&gt; {
  const { template, recipients } = job.data;
  // Build email from template
  // Send with nodemailer
  // Handle result
});
```
Inside the processor, use Nodemailer to send the actual email:
```javascript">// Create Nodemailer email options
const mailOptions = {
  from: 'Sender Name &lt;sender@email.com',
  to: recipients,
  subject: 'Your Daily Digest',
  html: output, // rendered template 
};

// Send email
transporter.sendMail(mailOptions, (error, info) =&gt; {
  if (error) {
    // handle error
  } else {
    // email sent!
  }
});
```
And that's the core process! The cron jobs add jobs to the queue, the queue processes them concurrently, uses the templates to generate email content, and then sends them with Nodemailer.

Make sure to handle any errors from Nodemailer sending appropriately. When done, you may also want to send a success/failure response back to the queue.

This allows the processing of many scheduled emails to smoothly and efficiently.
<h4>Handling Errors</h4>
It's important to properly handle any errors that occur during the email automation process.

For errors that happen when adding jobs to the queue from cron, you can try catching and logging the error:
```javascript">cron.schedule('...', () =&gt; {
  try {
    // add job 
  } catch (error) {
    // log error
  }
});
```
Inside the queue processor, you may want to listen for events like failed from Bull:
```javascript
emailQueue.on('failed', (error) =&gt; {
  // job failed, handle error
});
```
As mentioned, Nodemailer handles sending errors:
```javascript
transporter.sendMail(mailOptions, (error) =&gt; {
  if (error) {
    // log and handle error
  }
});
```
Based on the error, you may want to retry sending the email if it's transient. Or if an error continues occurring, pause the cron jobs and queue until you resolve.

Robust error handling will ensure failures don't cause the automation to go haywire.
<h4>Storing Email Logs</h4>
It can be useful to store logs of all scheduled emails sent through the system for analytics and records.

For example, you can use MongoDB and create an EmailLog schema:
```javascript
const EmailLogSchema = new Schema({
  timestamp: Date,
  recipient: String,
  template: String
});
const EmailLog = mongoose.model('EmailLog', EmailLogSchema);
```
Then, whenever an email is sent, save a log:
```javascript
// Inside queue processor
transporter.sendMail(options, (err) =&gt; {

  if (err) {
    // handle error
  } else {
    // email sent successfully 

    const log = new EmailLog({
      timestamp: new Date(),
      recipient: recipientEmail,
      template: templateName
    });
    log.save();
  }
});
```
Having these logs stored allows you to:
<ul>
 	<li>Analyze how many emails are being sent</li>
 	<li>See errors and trends</li>
 	<li>Optimize templates</li>
 	<li>Identify bad addresses by failures</li>
 	<li>And more!</li>
</ul>
<h4>Testing and Debugging</h4>
While developing your automated email process, testing and debugging thoroughly before deploying is helpful.

For testing, one easy technique is adding your email addresses to recipients and sending emails locally.

You can even use a tool like Mailhog to catch the emails sent from your local system during development.

Mailhog gives you a local SMTP server and a web UI to view the emails caught. Super useful for testing!

To further help with debugging, consider:
<ul>
 	<li>Logging key info like cron task running, jobs added, email sending success/failure, etc.</li>
 	<li><a class="attrlink" href="https://www.freecodecamp.org/news/how-to-enable-es6-and-beyond-syntax-with-node-and-express-68d3e11fe1ab/" target="_blank" rel="noopener noreferrer nofollow">Enabling ES6 stack traces in Node</a></li>
 	<li>Attaching request IDs and passing them through the system</li>
 	<li>Using a debugger tool like <a class="attrlink" href="https://github.com/GoogleChromeLabs/ndb" target="_blank" rel="noopener noreferrer nofollow"><strong>ndb</strong></a> to step through code</li>
</ul>
Taking time to test and debug your automation flow will pay off with a smoothly-running email scheduler in the long run.
<h4>Conclusion</h4>
And there we have it - a complete system for scheduled, automated emails with Node.js, Nodemailer, Bull, and Corn!

The key pieces are:
<ul>
 	<li>Nodemailer transports for sending from Node.js</li>
 	<li>Easy-to-reuse Handlebars email templates</li>
 	<li>A Bull queue for concurrent background processing</li>
 	<li>Cron jobs powered by node-cron to schedule emails</li>
 	<li>Handling and logging errors</li>
 	<li>Saving email logs to the database</li>
 	<li>Rigorous testing and debugging</li>
</ul>
This allows you to create recurring automated emails for your users, no matter the use case. Customizable templates and flexible scheduling opens up many possibilities.

A few ways you could extend the system further include:
<ul>
 	<li>Sending webhooks on failures to alert your team</li>
 	<li>Exposing APIs to initiate sending emails manually</li>
 	<li>Integrating with external event systems to trigger emails reactively</li>
 	<li>Building a front to manage templates and schedules</li>
 	<li>Adding deep analytics on sent emails and user engagement</li>
</ul>
Hopefully, this gives you a comprehensive foundation to build automated emails into your Node.js apps! Reach out if you have any other questions.
<h4><strong>Resource</strong></h4>
<ul>
 	<li>
<ul>
 	<li><a class="attrlink" href="https://blog.learnhub.africa/2023/01/18/how-to-send-an-email-in-node-js-using-nodemailer/" target="_blank" rel="noopener noreferrer nofollow">How to send an email in node.js using nodemailer</a></li>
 	<li><a class="attrlink" href="https://www.w3schools.com/js/js_arrays.asp" target="_blank" rel="noopener noreferrer nofollow">Javascript Array method</a></li>
 	<li><a class="attrlink" href="https://blog.learnhub.africa/2023/05/09/20-best-javascript-frameworks-for-2023/" target="_blank" rel="noopener noreferrer nofollow">20 Best React JavaScript Frameworks For 2023</a></li>
</ul>
</li>
</ul>