- Email DIY
The Difference Between SMTP and API
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.
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?
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?
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.
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.
Require the node modules and include nunjucks so you can add variables to your HTML email templates.
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 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 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’.
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.
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.
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.
It's easy to get started. And it's free.
See what you can accomplish with the world’s best email delivery platform.
Last updated on December 17, 2020
The Difference Between SMTP and API
The Basics of Email Dark Mode
COVID-19 Survey: How the Pandemic Has Affected Email Sending
International Women’s Day: How Pathwire’s Female Leaders Choose To Challenge
The Top Email Clients and Email Apps of 2021
The Benefits of Email Automation
Mailpets: For The Love Of Animals
The Mailgun Maverick Program Is Here!
How To Avoid Emails Going To Spam
Force for Change: It's Time to Speak Out
Easier and Faster Implementation with Our Updated SDKs
We stand with the AAPI community
The Difference Between SMTP and API
The Basics of Email Dark Mode
COVID-19 Survey: How the Pandemic Has Affected Email Sending
Mailgun Validations Features Improved Performance for EU Customers
International Women’s Day: How Pathwire’s Female Leaders Choose To Challenge
The Top Email Clients and Email Apps of 2021
How To Build An Email List The Right Way
The Path To Email Engagement In 2021: Key Learnings
We stand with the AAPI community
The Difference Between SMTP and API
The Basics of Email Dark Mode
Preparing Your Email Infrastructure Correctly
4 Tips To Improve Your Email Deliverability In 2020
COVID-19 Email Communications Dos and Don’ts
How To Use Parallel Programming
Mailgun’s COVID-19 Plan of Action
Password Meters Are Not For Humans
Send Your Emails at the Perfect Time with Send Time Optimization
Always be in the know and grab free email resources!
By sending this form, I agree that Mailgun may contact me and process my data in accordance with its Privacy Policy.