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

05 May, 2021
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: .  
It is easy to get started and easy to use.It is widely used and popular module for sending emails.Mails can also be scheduled.
It is easy to get started and easy to use.
It is widely used and popular module for sending emails.
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.  
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 command prompt using the command.  
After installing mailgun you can check your mailgun version in 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.  
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.  
Requiring module: You need to include mailgun module in your file by using these lines.  
const mailgun = require('mailgun-js');
Filename: index.js  
javascript
var API_KEY = 'Your_Api_Key';var DOMAIN = 'Your_Domain';var 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);  });} var sender_email = 'sender@gmail.com'var receiver_email = 'receiver@gmail.com'var email_subject = 'Mailgun Demo'var email_body = 'Greetings from geeksforgeeks' // User-defined function to send emailsendMail(sender_email, receiver_email,            email_subject, email_body)
Steps to run the program:  
The project structure will look like this:  
The project structure will look like this:  
Make sure you have installed mailgun using the following commands:  
Make sure you have installed mailgun using the following commands:  
npm install mailgun-js
Run index.js file using the following command:  
Run index.js file using the following command:  
node index.js
 
 
If no error occurs, then go and check the receiver email inbox  
If no error occurs, then go and check the receiver email inbox  
 
simmytarika5
Node.js-Misc
Node.js
Web Technologies
Web technologies Questions
Write From Home
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Comments
Old Comments
Express.js express.Router() Function
Node.js fs.readFile() Method
Node.js fs.writeFile() Method
How to install the previous version of node.js and npm ?
Difference between promise and async await in Node.js
Roadmap to Become a Web Developer in 2022
Top 10 Projects For Beginners To Practice HTML and CSS Skills
How to fetch data from an API in ReactJS ?
How to insert spaces/tabs in text using HTML/CSS?
Top 10 Angular Libraries For Web Developers