How to Send, Validate, and Track Emails with Mailgun in Node.js

Email is one of the most effective ways to connect with users, whether it’s sending a welcome message, validating an address, or tracking engagement. If you are building applications in Node.js, integrating a reliable email service is essential. In this guide, you will learn how to send, validate, and track emails with Mailgun in Node.js step by step.

Why Do We Need Mailgun?

When building an application, you need more than just the ability to send emails. You must ensure that emails reach the inbox, addresses are real, and engagement is measurable.

Traditional SMTP servers often fail to handle these needs while Mailgun solves this problem by offering advanced features like domain verification, analytics, validations, and tracking so your email system remains professional and reliable and protects your email from marking as spams.

Advantages of Mailgun

  • High deliverability and trusted reputation
  • Powerful APIs for sending, validating, and tracking emails
  • Real-time analytics for opens, clicks, and deliveries
  • Easy integration with Node.js and other platforms
  • Scalable for startups, SaaS, and large scale enterprises applications

Generating Mailgun API Key

To get started, visit the Mailgun website and create a new account. Once you have signed up, you’ll need to verify your email address and log in to the Mailgun dashboard.

After logging in, the next step is to add your sending domain. Mailgun requires you to verify domain ownership by updating DNS records provided in the dashboard. Once the records are correctly set up and verified, your domain will be ready to send emails.

If you are only testing or working in local development, you don’t need to verify your own domain right away. Mailgun provides a default sandbox domain (for example, sandbox12345.mailgun.org) that you can use for testing. With the sandbox domain, you can send test emails but only to addresses you add as authorized recipients in the dashboard. This is perfect for trying out sending, validating, and tracking emails during development.

To generate API key, Navigate to Setting -> Security. Generate new API key, it will ask you to provide label or app name for you project. Once entered name for application, click on generate. It will show something like below:

how to use Mailgun in Node.js to send, validate, and track emails node JS

Store this key somewhere safe, we will use this into upcoming examples. Mailgun will not show this key again you have to generate new one it lost.

Install Mailgun Package for Node.js

To interact with Mailgun from your Node.js application, you need to install the official Mailgun client. This package makes it simple to send, validate, and track emails using Mailgun’s REST API. Along with the client, we also install the form-data package, which helps in handling HTTP form submissions required by Mailgun.

Run the following command in your project directory:

npm install mailgun.js form-data

Once installed, you can import and configure the Mailgun client in your application by passing your private API key and domain.

Sending an Email using Mailgun in Node.js

Imagine you are working on authentication functionality, and want to send an email when user registers for your node.js application. Most of the cases it common requirement. Let’s send an welcome email to user using Mailgun:

const formData = require('form-data');
const Mailgun = require('mailgun.js');

const mailgun = new Mailgun(formData);
const mg = mailgun.client({ username: 'api', key: 'YOUR_API_KEY' });

mg.messages.create('YOUR_DOMAIN', {
  from: "My App <mail@codewolfy.com>",
  to: ["newuser@codewolfy.com"],
  subject: "Welcome to Codewolfy",
  text: "We are excited to have you on board!",
  html: "<h1>Welcome!</h1><p>We are excited to have you on board.</p>"
})
.then(msg => console.log(msg))
.catch(err => console.error(err));

For sending mail, you will first import required package like form-data and mailgun then create new instance of mailgun and configure your API key and domain. Here we have taken static values for mail content but you make it dynamic based on your requirements.

Validating an Email using MailGun

You can utilize Mailgun for email validation service for provided by user while register or subscribing to your newsletter. It will reduce dummy signups and provides quality data.

const formData = require('form-data');
const Mailgun = require('mailgun.js');

const mailgun = new Mailgun(formData);
const mg = mailgun.client({ username: 'api', key: 'YOUR_API_KEY' });

mg.validate.get('test@mailinator.com')
.then(result => console.log(result))
.catch(err => console.error(err));

This ensures that disposable or invalid email addresses are rejected.

Tracking Emails with MailGun

For e-commerce store and send order confirmation emails, you may want to track whether customers open them.

Enable tracking in your Mailgun domain settings, then use web hooks in your Node.js app:

const express = require('express');
const app = express();

app.use(express.json());

app.post('/mailgun/webhook', (req, res) => {
  console.log(req.body);
  res.sendStatus(200);
});

app.listen(3000, () => console.log("Webhook server running on port 3000"));

With tracking, you can monitor events like opened, clicked, or delivered emails.

Conclusion

Learning how to send, validate, and track emails with Mailgun in Node.js gives you complete control over your application’s communication system. From welcome messages to order confirmations, Mailgun ensures high deliverability, accurate validation, and real-time tracking. Mailgun provides the right tools to make your email system efficient and trustworthy.