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...



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.

  1. /

  2. /submit/:mail

  3. /validate/:mail

  4. /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.


Save the code below as index.jade within the /views directory

Save the code below as error.jade within the /views directory


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

And that’s how you get started sending transactional email on demand!


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?

Related readings

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

What is a RESTful API, how it works, advantages, and examples

RESTful APIs are the most commonly used APIs in the world of web services. They use...

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