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

mailgunplus

v0.6.1

Published

Mailgun for Node.js

Downloads

6

Readme

node-mailgun

This library provides simple access to Mailgun's API for node.js applications. It's MIT licensed, and being used in production in my app Complice.

It was forked from shz's package mailgun, and at the moment works the same except it has more details in error messages. I've been bolting on improvements in the Complice code for awhile, but for this one I needed to modify the source of this package. I may include other improvements here later—particularly a sendHtml method.

Note that most of what's written below is from shz in 2014. I don't vouch for its accuracy.

Installation

$ npm install mailgunplus

Or you can just throw mailgun.js into your application. There are no dependencies outside of node's standard library.

Usage

At the time of writing, Mailgun's documentation is actually incorrect in places, which is unfortunate. As such, I'm going to re-document everything in this README according to the actual way it's implemented in node-mailgun, which itself is based off the implementation from Mailgun's github account, and not the API docs on the site.

Initialization

Access to the API is done through a Mailgun object. It's instantiated like so:

var Mailgun = require('mailgunplus').Mailgun;
var mg = new Mailgun('api-key');

Sending Email

Mailgun's API provides two methods for sending email: raw, and text. Both of them are exposed here.

sendText

Sends a simple plain-text email. This also allows for slightly easier sending of Mailgun options, since with sendRaw you have to set them in the MIME body yourself.

sendText(sender, recipients, subject, text, [servername=''], [options={}], [callback(err)])
  • sender - Sender of the message; this should be a full email address (e.g. [email protected]).
  • recipients - A string ([email protected]) or array of strings (['[email protected]', '[email protected]']) of recipients; these can be email addresses or HTTP URLs.
  • subject - Message subject
  • text - Message body text
  • servername - The name of the Mailgun server. If you only have one server on your Mailgun account, this can be omitted. Otherwise, it should be set to the server you want to send from.
  • options - Optional parameters. See Mailgun's API docs for details on these. At the time of writing, the only supported value is headers, which should be a hash of additional MIME headers you want to send.
  • callback - Callback to be fired when the email is done being sent. This should take a single parameter, err, that will be set to the status code of the API HTTP response code if the email failed to send; on success, err will be undefined.

Example


sendText('[email protected]',
         ['[email protected]', 'http://example.com/recipient2'],
         'Behold the wonderous power of email!',
         {'X-Campaign-Id': 'something'},
         function(err) { err && console.log(err) });

sendRaw

Sends a raw MIME message. Don't just use this with text; instead, you should either build a MIME message manually or by using some MIME library such as andris9's mailcomposer module https://github.com/andris9/mailcomposer (FWIW mailcomposer is the same module used by the popular nodemailer module http://github.com/andris9/Nodemailer).

sendRaw(sender, recipients, rawBody, [servername], [callback(err)])
  • sender - Sender of the message; this should be a full email address (e.g. [email protected])
  • recipients - A string ([email protected]) or array of strings (['[email protected]', '[email protected]']) of recipients; these can be email addresses or HTTP URLs.
  • rawBody - MIME message to send
  • servername - The name of the Mailgun server. If you only have one server on your Mailgun account, this can be omitted. Otherwise, it should be set to the server you want to send from.
  • callback - Callback to be fired when the email is done being sent. This should take a single parameter, err, formatted as {status: Number, res: Object, message: String} if the email failed to send; on success, err will be undefined.

Example

sendRaw('[email protected]',
        ['[email protected]', 'http://example.com/recipient2'],
        'From: [email protected]' +
          '\nTo: ' + '[email protected], http://example.com/recipient2' +
          '\nContent-Type: text/html; charset=utf-8' +
          '\nSubject: I Love Email' +
          '\n\nBecause it\'s just so awesome',
        function(err) { err && console.log(err) });

Email Addresses

Mailgun allows sender and recipient email addresses to be formatted in several different ways:

Mailgun Headers

Mailgun understands a couple special headers, specified via options when using sendText, or in the MIME headers when using sendRaw. These are defined below.

  • X-Mailgun-Tag - Used to tag sent emails (defined in Mailgun.MAILGUN_TAG)
  • X-Campaign-Id - Used for tracking campaign data (defined in Mailgun.CAMPAIGN_ID)

Example

Here's a complete sending example.

var Mailgun = require('mailgun').Mailgun;

var mg = new Mailgun('some-api-key');
mg.sendText('[email protected]', ['Recipient 1 <[email protected]>', '[email protected]'],
  'This is the subject',
  'This is the text',
  '[email protected]', {},
  function(err) {
    if (err) console.log('Oh noes: ' + err);
    else     console.log('Success');
});

Routing

Mailgun lets you route incoming email to different destinations. TODO - more docs

createRoute

Creates a new route. TODO - more docs

createRoute(pattern, destination, [callback(err, id)])

TODO - document arguments

deleteRoute

Deletes the route with the specified ID if it exists, otherwise fails silently.

deleteRoute(id, [callback(err)])
  • id - Route ID, as returned by getRoutes() or createRoute.
  • Callback to be fired when the deletion is completed. This callback takes a single argument, err, that will be set to an Error object if something went wrong with the deletion. If the deletion succeeded, or no route existed with the specified ID, err will be undefined.

getRoutes

Gets a list of all routes.

getRoutes(callback(err, routes))
  • callback - Callback to be fired when the request has finished. This should take two parameters: err, which will hold either an HTTP error code, or an error string on failure; and routes, which will be a list of routes on success. Routes returned through this callback will be objects with three fields: pattern, destination, and id.

Example

getRoutes(function(err, routes) {

  if (err) console.log('Error:', err);

  for (var i=0; i<routes.length; i++) {
    console.log('Route');
    console.log('  Pattern:', routes[i].pattern);
    console.log('  Destination:', routes[i].destination);
    console.log('  Id:', routes[i].id);
  }
});

Eventual Work:

  • Mailboxes