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

How to send bulk email: The dos and don’ts of mass email sending

Both transactional and marketing email messages can rely on sending massive amounts of email all at once. But while bulk is better when buying toilet paper, that’s not always the...

Read more

Best practices for successful email delivery

Composing good email is important but making sure it successfully reaches your recipients is critical. So many well-intended messages end up in spam (60% in a recent analysis)...

Read more

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

Popular posts

Email inbox.

Build Laravel 10 email authentication with Mailgun and Digital Ocean

When it was first released, Laravel version 5.7 added a new capability to verify user’s emails. If you’ve ever run php artisan make:auth within a Laravel app you’ll know the...

Read more

Mailgun statistics.

Sending email using the Mailgun PHP API

It’s been a while since the Mailgun PHP SDK came around, and we’ve seen lots of changes: new functionalities, new integrations built on top, new API endpoints…yet the core of PHP...

Read more

Statistics on deliverability.

Here’s everything you need to know about DNS blocklists

The word “blocklist” can almost seem like something out of a movie – a little dramatic, silly, and a little unreal. Unfortunately, in the real world, blocklists are definitely something you...

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