Mailgun in Javascript

Posted by Joe Enos on August 17, 2021 · 1 min read

Mailgun is a great email service with reasonable pricing, including a fantastic free tier.

Once you set up the account, sending email from NodeJS is incredibly easy.

var api_key = 'MY_API_KEY';
var domain = 'mg.MYDOMAIN.com';
var mailgun = require('mailgun-js')({apiKey: api_key, domain: domain});

var data = {
from: 'Some User <SOMEUSER@MYDOMAIN.com>',
to: 'person1@example.com,person2@example.com',
subject: 'Hello',
text: 'This is a test!',
html: 'This is a <b>test</b>!'
};

mailgun.messages().send(data, function (error, body) {
console.log(body);
});