npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

nodemailer-mailgun-transport-esm

v1.0.0

Published

A transport module to use with nodemailer to leverage Mailgun's REST API

Downloads

7

Readme

nodemailer-mailgun-transport

Known Vulnerabilities

What is this?

nodemailer is an amazing node module to send emails within any of your nodejs apps. This is the transport plugin that goes with nodemailer to send email using Mailgun's awesomeness! Pow Pow.

How does it work?

Based on this mailgun-js module and the nodemailer module, the Mailgun Transport was born. This is a transport layer, meaning it will allow you to send emails using nodemailer, using the Mailgun API instead of the SMTP protocol!

Nodemailer allows you to write code once and then swap out the transport so you can use different accounts on different providers. On top of that it's a super solid way of sending emails quickly on your node app(s).

The Mailgun transport for nodemailer is great to use when SMTP is blocked on your server or you just prefer the reliability of the web api!

Quickstart - Example

Create a new file, install the dependencies [1] and look at the skeleton code below to get you started quickly!

import nodemailer from 'nodemailer';
import mg from 'nodemailer-mailgun-transport-esm';

// This is your API key that you retrieve from www.mailgun.com/cp (free up to 10K monthly emails)
const auth = {
  auth: {
    api_key: 'key-1234123412341234',
    domain: 'one of your domain names listed at your https://app.mailgun.com/app/sending/domains'
  }
}

const nodemailerMailgun = nodemailer.createTransport(mg(auth));

nodemailerMailgun.sendMail({
  from: '[email protected]',
  to: '[email protected]', // An array if you have multiple recipients.
  cc:'[email protected]',
  bcc:'[email protected]',
  subject: 'Hey you, awesome!',
  'replyTo': '[email protected]',
  //You can use "html:" to send HTML email content. It's magic!
  html: '<b>Wow Big powerful letters</b>',
  //You can use "text:" to send plain-text content. It's oldschool!
  text: 'Mailgun rocks, pow pow!'
}, (err, info) => {
  if (err) {
    console.log(`Error: ${err}`);
  }
  else {
    console.log(`Response: ${info}`);
  }
});

Buffer support

Example:

const mailOptions = {
    ...
    attachments: [
        {
            filename: 'text2.txt',
            content: new Buffer('hello world!','utf-8')
        },

with encoded string as attachment content:

const mailOptions = {
    ...
    attachments: [
        {
            filename: 'text1.txt',
            content: 'aGVsbG8gd29ybGQh',
            encoding: 'base64'
        },

with encoded string as an inline attachment:

// Replace `filename` with `cid`
const mailOptions = {
    ...
    attachments: [
        {
            cid: 'logo.png',
            content: 'aGVsbG8gd29ybGQh',
            encoding: 'base64'
        },
<!-- Reference the `cid` in your email template file -->
<img src="cid:logo.png" alt="logo" />

Address objects

The "from", "to", "cc", and "bcc" fields support an address object or array of address objects. Each "name" and "address" are converted to "name <address>" format. "name" is optional, "address" is required. Missing or null address in object is skipped.

Examples:

 from: {name: 'Sales', address: '[email protected]'},
 to: [{name:'Mary', address:'[email protected]'}, {address:'[email protected]'}]

is converted to:

  from: 'Sales <[email protected]>',
  to: 'Mary <[email protected]>,[email protected]'

Now with Consolidate.js templates, and also compatible with mailgun's own templates

This package has two options for templating - one is to allow mailgun's templating engine to process the template, and the other is to use templates in your own codebase using any templating engine supported by consolidate.js.

To use mailgun's templating engine (and allow templates to iterate independent of your codebase), simply pass the template name to the template key, and the template variables as a stringified JSON to the h:X-Mailgun-Variables key. Here's an example: 

nodemailerMailgun.sendMail({
  from: '[email protected]',
  to: '[email protected]', // An array if you have multiple recipients.
  subject: 'Hey you, awesome!',
  template: 'boss_door',
  'h:X-Mailgun-Variables': JSON.stringify({key:'boss'})
}, (err, info) => {
  if (err) {
    console.log(`Error: ${err}`);
  }
  else {
    console.log(`Response: ${info}`);
  }
});

To use consolidate.js templates locally, give the template key an object instead that contains a name key, an engine key and, optionally, a context object. For example, you can use Handlebars templates to generate the HTML for your message like so:

const contextObject = {
  variable1: 'value1',
  variable2: 'value2'
};

nodemailerMailgun.sendMail({
  from: '[email protected]',
  to: '[email protected]', // An array if you have multiple recipients.
  subject: 'Hey you, awesome!',
  template: {
    name: 'email.hbs',
    engine: 'handlebars',
    context: contextObject
  }
}, (err, info) => {
  if (err) {
    console.log(`Error: ${err}`);
  }
  else {
    console.log(`Response: ${info}`);
  }
});

You can use any of the templating engines supported by Consolidate.js. Just require the engine module in your script, and pass a string of the engine name to the template object. Please see the Consolidate.js documentation for supported engines.

Mailgun Regions

You can use two different region environments for your mailgun domains. For USA region you should use api endpoint api.mailgun.net, but for EU region api.eu.mailgun.net

You can pass it as "host" to transport options object:

import nodemailer from 'nodemailer';
import mg from 'nodemailer-mailgun-transport-esm';

const options = {
  auth: {
    api_key: 'key-1234123412341234',
    domain: 'one of your domain names listed at your https://mailgun.com/app/domains'
  },
  host: 'api.eu.mailgun.net' // e.g. for EU region
}

const nodemailerMailgun = nodemailer.createTransport(mg(auth));

[1] Quickly install dependencies

npm install --save nodemailer nodemailer-mailgun-transport-esm