Back to main menu

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

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:

  • Badge Check

    Confirmation emails

  • Badge Check

    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:

  • Badge Check

    Sending a single transactional email

  • Badge Check

    Sending a newsletter to an email list

  • Badge Check

    Adding email addresses to a list

  • Badge Check

    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.

  1. Badge Check

    /

  2. Badge Check

    /submit/:mail

  3. Badge Check

    /validate/:mail

  4. Badge Check

    /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:

  • Badge Check

    Registering an account on a site and Hello message email

  • Badge Check

    Resetting a password

  • Badge Check

    Confirming purchases or critical actions (deleting an account)

  • Badge Check

    Two-factor authentication with multiple email addresses

What do you use transactional emails for in your day-to-day business?

Related readings

Key rotation best practices: Keeping your email program secure

Many of us have been managing passwords for decades. Maybe it started with your Neopets account, but somewhere in the early 90s, password protection joined the conversation and never left.  

Read More

Email validation – Why is it vital for your inbox?

We all have those moments when we get nervous and need to double-check ourselves. Did we fill in the right answer bubbles on a test? Did we type in the right password when...

Read More

How to improve holiday supply chain communication with email

This year, shoppers aren’t the only ones worried about orders arriving on time for the holidays. The 2023 holiday shopping season is packed with global supply chain...

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