Email Development
How to send transactional email in a NodeJS app using the Mailgun API
Sending transactional emails is easy regardless of your tools. If you use a NodeJS helper library, this walkthrough will help you get set up in Mailgun. Read more...
PUBLISHED ON
Not only you can get powerful re-engagement based on triggers, actions, and patterns you can also communicate important information and most importantly, automatically between your platform and a customer.
It’s highly likely that during your life as a developer you’ll have to send automated transactional emails, if you haven’t already, like:
Confirmation emails
Password reminders
…and many other kinds of notifications.
In this tutorial, we’re going to learn how to send transactional emails using the Mailgun API and using a NodeJS helper library.
We will cover different scenarios:
Sending a single transactional email
Sending a newsletter to an email list
Adding email addresses to a list
Sending an invoice to a single email address
Getting started
We will assume you have installed and know how to operate within the NodeJS environment.
The first need we’re going to do is generate a package.json
file in a new directory (/mailgun_nodetut
in my case).
This file contains details such as the project name and required dependencies.
As you can see, we’re going to use expressjs
for our web-app scaffolding, with jade
as a templating language and finally a community-contributed library for node by bojand.
In the same folder where you’ve created the package.json
file create two additional folders:
views/
js/
You’re set – now sign in to Mailgun, it’s free if you haven’t done it yet and get your API key (first page in the control panel).
Set up a simple ExpressJS app
Save the code below as app.js
in the root directory of your app (where package.json
is located)
How it works
This above is a simple express app that will run on your local machine on port 3030
. We have defined it to use expressjs and the mailgun-js wrapper. These are pretty well-known libraries that will help us quickly set up a small website that will allow users to trigger the sending of email addresses to their address.
First things first
We’ve defined 4 endpoints.
/
/submit/:mail
/validate/:mail
/invoice/:mail
Where :mail
is your valid email address.
When navigating to an endpoint, Express will send to the browser a different view.
In the background, Express will take the input provided by the browser and process it with the help of the mailgun-js
library.
In the first case, we will navigate to the root that is localhost:3030/
You can see there’s an input box requesting your email address. By doing this, you send yourself a nice transactional email.
This is because expressjs takes your email address from the URL parameter and by using your API key and domain does an API call to the endpoint to send emails.
Each endpoint will invoke a different layout, I’ve added the code of basic layouts below, feel free to add and remove stuff from it.
Layouts
Save the code below as index.jade
within the /views directory
Save the code below as error.jade
within the /views directory
Javascript
This code allows the browser to call a URL from the first form field with your specified email address appended to it. This way we can simply parse the email address from the URL with express’ parametered route syntax.
Save the code below as main.js
within the /js directory
Finally, create an empty invoice.txt
file in your root folder. This file will be sent as an attachment.
Now run npm install
(or sudo npm install
in some cases depending on your installation) to download dependencies, including Express.
Once that’s done run node app.js
Navigate with your browser to localhost:3030
(or 0.0.0.0:3030
)
And that’s how you get started sending transactional email on demand!
Conclusion
In our example, we’ve seen how you can send a transactional email automatically when the user triggers a specific action, in our specific case, submitting the form.
There are thousands of applications that you could adapt this scenario to. Sending emails can be used for:
Registering an account on a site and Hello message email
Resetting a password
Confirming purchases or critical actions (deleting an account)
Two-factor authentication with multiple email addresses
What do you use transactional emails for in your day-to-day business?