Introduction to Mailgun.js
Mailgun.js is a powerful and flexible JavaScript library designed for seamless integration with the Mailgun email service API. This library allows developers to easily send and receive emails, manage mailing lists, handle email verifications, and much more. In this comprehensive guide, we’ll explore dozens of useful API examples and code snippets to help you get started and make the most out of your email integration.
Getting Started with Mailgun.js
First, let’s install the Mailgun.js library using npm:
$ npm install mailgun-js
Once installed, start by configuring your Mailgun credentials:
const mailgun = require('mailgun-js')({apiKey: 'YOUR_API_KEY', domain: 'YOUR_DOMAIN_NAME'});
Sending an Email
Sending an email with Mailgun.js is straightforward:
const data = {
from: 'Excited User <me@samples.mailgun.org>',
to: 'bar@example.com, YOU@YOUR_DOMAIN_NAME',
subject: 'Hello',
text: 'Testing some Mailgun awesomeness!'
};
mailgun.messages().send(data, (error, body) => {
console.log(body);
});
Handling Incoming Emails
Mailgun.js can also be used to handle incoming emails. First, set up a route:
mailgun.routes().create({
expression: 'match_recipient(".*@YOUR_DOMAIN_NAME")',
action: 'store(notify="https://example.com/mailgun-webhook")',
description: 'Store all incoming messages'
}, (error, body) => {
console.log(body);
});
Validating Email Addresses
Ensuring valid email addresses is crucial for effective email delivery. Mailgun.js provides a simple way to validate emails:
mailgun.validate('test@example.com', (error, body) => {
console.log(body);
});
Managing Mailing Lists
Mailgun.js allows you to manage your mailing lists efficiently:
// Create a new mailing list
mailgun.lists().create({address: 'developers@YOUR_DOMAIN_NAME'}, (error, body) => {
console.log(body);
});
// Add a member to the list
mailgun.lists('developers@YOUR_DOMAIN_NAME').members().create({
subscribed: true,
address: 'user@example.com'
}, (error, body) => {
console.log(body);
});
// List all members
mailgun.lists('developers@YOUR_DOMAIN_NAME').members().list((error, body) => {
console.log(body);
});
Example Application
Let’s put it all together and create a simple Node.js application that sends a welcome email to new users:
const express = require('express');
const bodyParser = require('body-parser');
const app = express();
app.use(bodyParser.json());
// Your Mailgun configuration
const mailgun = require('mailgun-js')({apiKey: 'YOUR_API_KEY', domain: 'YOUR_DOMAIN_NAME'});
app.post('/signup', (req, res) => {
const {email} = req.body;
// Send welcome email
const data = {
from: 'Support <support@YOUR_DOMAIN_NAME>',
to: email,
subject: 'Welcome!',
text: 'Thank you for signing up. We are excited to have you!'
};
mailgun.messages().send(data, (error, body) => {
if (error) {
res.status(500).send('Error sending email');
} else {
res.status(200).send('Welcome email sent');
}
});
});
app.listen(3000, () => {
console.log('Server is running on port 3000');
});
This application listens for new user signups and sends a welcome email to the email address provided, utilizing the power of Mailgun.js.
Hash: 0e6debbb2dcb79b425ebe28338ba37f49ad9af03448030fe5416e513d6a4871b