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.



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

8 benefits of email automation you should definitely know

You’ve built a great app or product and added an email functionality. But what next? How can you improve your user’s email interaction with your app? Don’t worry; this doesn’t...

Read more

SMTP v. API: Their differences and how to use them

You’re tasked with integrating email into the app you’re building or the customer relationship management (CRM) tool your business uses. You have two choices: SMTP or...

Read more

Tips and tricks for scheduling email delivery

Picture this: You're on vacation, but your email marketers don't want to miss the optimal window to send an email to your subscribers. They need you to get the email out, but...

Read more

Popular posts

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