Back to main menu

Email

Scheduled email delivery with Mailgun and Node.Js

Authored by Timothy Voice, a Brooklyn based software developer and MEAN.JS enthusiast. This post was originally posted on his blog.

PUBLISHED ON

PUBLISHED ON

This is an updated version of the post “Scheduled Email Delivery With Mailgun And Node.Js” written by Timothy Voice, a Brooklyn based software developer and MEAN.JS enthusiast. The original post was first posted on his blog.

Usually, most of us don't like stuff that's mean (unless it's Mean Girls, maybe). However, there are a few "mean" things that can actually be pretty helpful. One of these is the MEAN (MongoDB, Express.js, AngularJS, and Node.js) app, which allows you to build websites and mobile apps. Want to learn more? Let's dive in.

What is scheduled email delivery?

Scheduled email delivery is a fairly recent invention, but it’s one that has quickly reached the mainstream when it comes to email messages. It’s common with most major internet service providers like Microsoft Outlook, Gmail, and Yahoo, and it allows you to delay delivery of your messages until a later time. When you compose a message, you may want it to be delivered at a specified time instead of immediately being sent. Scheduling email allows you to keep your messages in your outbox until you want them to be sent. Simple, right?

Why do people schedule emails for later times?

People send emails for a variety of reasons (trust us, we know). Sometimes, if coworkers are collaborating across different time zones, they may want to delay delivery until the recipient has started their workday so that the email will be top-of-mind. Other times, people may want to defer delivery when they have ideas or tasks they want to follow-up on later. The ability to delay sending for a specific amount of time enables them to remind themselves of what they want to accomplish with a new email. 

Of course, scheduled email delivery doesn’t happen on its own. So where does MEAN come in?

Why is MEAN important to scheduled email delivery?

When it comes to capabilities, your awesome MEAN app you’ve created can help you send transactional email to support your overall service such as email confirmations, forgotten passwords, notifications, and so on - this, of course, includes email delivery options.. There is a class of apps where email is the service, such as weather apps, newsletter delivery, etc., and that’s where scheduled email delivery comes in. For these services, we need our app to email users automatically at given times and under certain conditions. By combining some great npm modules and hooking into an email service, this can be quickly achieved.

How do you set up MEAN with Mailgun to achieve scheduled email delivery?

To add scheduled email functionality to your MEAN app, follow the steps below.

You will need 4 functions: A mailUsers function to select recipients, a mailCreator function to generate the email from a template, a mailScheduler function to trigger the mailing at a certain time, and a mailSender function to send the emails.

The Setup

Require the node modules and include nunjucks so you can add variables to your HTML email templates.

Querying the Users

We’re using the mongoose find(x).where(y) query to sift through the users and find those who have selected to receive notifications on the given day and returning a promise.

The Mail Creator

The mailCreator takes in an array of users and generates another array that contains the user email addresses and the html template rendered by nunjucks.

The Mailer

The mailSender function will take in a user email, subject, and html template for our email and return a promise that, when resolved, contains the response from Mailgun’s API; something like ‘Queued. Thank you’.

The Scheduler

Here, your scheduler function takes in the job we want done and runs it when the rule variable and rule description dictates. For example: using a node scheduler everyday to schedule for daily send at 4:38PM. See here for recurrence rule making in node-scheduler.

Putting it all together

Now, all you need to do is call the scheduler function within the delivery nodes controller, passing in the users your query returned, your mail generator, and mail sender. You can find the whole system in one node controller here. Here is an example scheduled mailing call:

Now, you have a controller that will find the users that have selected to receive notifications for a given time, and send them an email when your job-scheduler fires.

This controller will enable you to apply rules for sending, manage delivery, and more. Through this MEAN app integration, you’ll be able to schedule emails with ease.

Wrapping up

While figuring out the ins and outs of scheduled email delivery can be tricky, using a MEAN app integration with Mailgun can help you take charge of your email scheduling and send your emails exactly when they need to go out. And that’s pretty fetch.

Sign Up

It's easy to get started. And it's free.

See what you can accomplish with the world’s best email delivery platform.

Related readings

Verifying emails with node.js

If you're looking to validate a batch of email addresses, Mailgun offers a bulk email validation API that can help you do just that. In this tutorial, you'll learn how to efficiently verify large lists of email addresses in Node.js applications...

Read More

Mailgun for non-devs: Leveraging an email marketing platform

This guest post comes from Ongage, a front-end email marketing platform that integrates nicely with ESPs like Mailgun and empowers...

Read More

Email remains the most effective marketing platform

This article was written and contributed by Rod Ussing at Lookahead.io...

Read More

Popular posts

Email inbox.

Email

5 min

Build Laravel 11 email authentication with Mailgun and Digital Ocean

Read More

Mailgun statistics.

Product

4 min

Sending email using the Mailgun PHP API

Read More

Statistics on deliverability.

Deliverability

5 min

Here’s everything you need to know about DNS blocklists

Read More

See what you can accomplish with the world's best email delivery platform. It's easy to get started.Let's get sending
CTA icon