Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • NodeJS Tutorial
  • NodeJS Exercises
  • NodeJS Assert
  • NodeJS Buffer
  • NodeJS Console
  • NodeJS Crypto
  • NodeJS DNS
  • NodeJS File System
  • NodeJS Globals
  • NodeJS HTTP
  • NodeJS HTTP2
  • NodeJS OS
  • NodeJS Path
  • NodeJS Process
  • NodeJS Query String
  • NodeJS Stream
  • NodeJS String Decoder
  • NodeJS Timers
  • NodeJS URL
  • NodeJS Interview Questions
  • NodeJS Questions
  • Web Technology
Open In App
Next Article:
How to send Attachments and Email using nodemailer in Node.js ?
Next article icon

How to Send Email using Mailgun API in Node.js ?

Last Updated : 25 Apr, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

Sending an email is an essential part of any project and it can be achieved by using Mailgun API. It is very popular for sending emails.

Features of Mailgun: . 

  1. It is easy to get started and easy to use.
  2. It is a widely used and popular module for sending emails.
  3. Mails can also be scheduled.

Installation of Mailgun module:  

You can visit the link to Install mailgun module. You can install this package by using the following command. 

npm install mailgun-js

After installing mailgun you can check your mailgun version in the command prompt using the command. 

npm version mailgun-js

After that, you can create a folder and add a file. For example index.js. To run this file you need to run the following command. 

node index.js

Requiring module: You need to include mailgun module in your file by using these lines. 

const mailgun = require('mailgun-js');

Project Structure:

project structure

Filename: 

index.js 

javascript

let API_KEY = 'Your_Api_Key';
let DOMAIN = 'Your_Domain';
const mailgun = require('mailgun-js')
    ({ apiKey: API_KEY, domain: DOMAIN });
 
sendMail = function (sender_email, receiver_email,
    email_subject, email_body) {
 
    const data = {
        "from": sender_email,
        "to": receiver_email,
        "subject": email_subject,
        "text": email_body
    };
 
    mailgun.messages().send(data, (error, body) => {
        if (error) console.log(error)
        else console.log(body);
    });
}
 
let sender_email = '[email protected]'
let receiver_email = '[email protected]'
let email_subject = 'Mailgun Demo'
let email_body = 'Greetings from geeksforgeeks'
 
// User-defined function to send email
sendMail(sender_email, receiver_email,
    email_subject, email_body)
                      
                       

Steps to run the program: 

Make sure you have installed mailgun using the following commands: 

npm install mailgun-js

Run the index.js file using the following command: 

node index.js

Output of above command

If no error occurs, then go and check the receiver’s email inbox  

receiver-inbox 



Next Article
How to send Attachments and Email using nodemailer in Node.js ?
author
gouravhammad
Improve
Article Tags :
  • Node.js
  • Web Technologies
  • Write From Home
  • Node.js-Misc

Similar Reads

  • How to Send Email using NodeJS?
    Sending emails programmatically is a common requirement in many applications, especially for user notifications, order confirmations, password resets, and newsletters. In this article, we will learn how to build a simple email-sending system using NodeJS. We will use Nodemailer, a popular module for
    5 min read
  • How to send Attachments and Email using nodemailer in Node.js ?
    For this purpose, we will use a package called nodemailer. It is a module that makes email sending pretty easy. For using it, you will need to install by using the following command:  $ npm install nodemailer Features of nodemailer module:  It has zero dependencies and heavy security.You can use it
    2 min read
  • How to Send an Image using Ajax ?
    Ajax stands for Asynchronous Javascript and XML and is used to make indirect requests to other origins. It can help you to perform a change to the web page dynamically. We can make an AJAX request with a special object called XMLHttpRequest which provides us with different methods to create an HTTP
    5 min read
  • How to write e-mails in HTML and send it using Gmail ?
    In this article, we are going to learn that how users can write e-mails in HTML format and send them using Gmail. However, Gmail doesn't offer an HTML editor still we can send HTML templates in an e-mail using some tools and methods. Many people need to send an e-mail with HTML templates to others.
    3 min read
  • How to Send Email using 'git send-email' via Gmail?
    Email communication is a crucial aspect of collaborative software development. Whether you're sharing patches, discussing issues, or coordinating with your team, knowing how to send emails directly from your Git repository can be highly efficient. In this article, we will guide you through the proce
    3 min read
  • Sending bulk emails in Node.js using SendGrid API
    What is SendGrid API? SendGrid is a platform for sending transactional and marketing emails to the customers. It provides scalability, reliability, and deliverability which is an important issue for an organization.Benefits of using SendGrid API: If you are using Nodemailer with Gmail then you can s
    2 min read
  • Sending SMS using NEXMO API in Node.js
    Introduction: SMS is a common method of sending short messages between cell phones, but these SMS can be sent using API in Node.js. Now there are lots of API in the market for sending SMS like Twilio, Exotel, etc to the user but the popular among them is Nexmo. Features of NEXMO: Integrating this mo
    2 min read
  • How to send email with Nodemailer using Gmail account in Node ?
    Nodemailer is an npm module that allows you to send emails easily from the backend. In this article, we will cover the steps to send email using a Gmail account with the help of nodemailer. Prerequisites:NPM and NodeJSExpressJSApproach To send email with Nodemailer using gmail Import the nodemailer
    3 min read
  • How to Upload File using formidable module in Node.js ?
    A formidable module is used for parsing form data, especially file uploads. It is easy to use and integrate into your project for handling incoming form data and file uploads. ApproachTo upload file using the formidable module in node we will first install formidable. Then create an HTTP server to a
    2 min read
  • How to Post Data in MongoDB Using NodeJS?
    In this tutorial, we will go through the process of creating a simple Node.js application that allows us to post data to a MongoDB database. Here we will use Express.js for the server framework and Mongoose for interacting with MongoDB. And also we use the Ejs for our front end to render the simple
    5 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences