| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
| Name | Name | Last commit date | ||
|---|---|---|---|---|
This library provides simple access to Mailgun's API for node.js applications. It's MIT licensed, and being used in production over at Hipsell.
$ npm install mailgunOr you can just throw mailgun.js into your application. There are no dependencies outside of node's standard library.
Note: master on Github is going to be untested/unstable at times, as this is a small enough library that I don't want to bother with a more complicated repo structure. As such, you should really only rely on the version of mailgun in npm, as I'll only ever push stable and tested code there.
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.
Access to the API is done through a Mailgun object. It's instantiated like so:
var Mailgun = require('mailgun').Mailgun;
var mg = new Mailgun('api-key');Mailgun's API provides two methods for sending email: raw, and text. Both of them are exposed here.
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)])sendText('sender@example.com',
['recipient1@example.com', 'http://example.com/recipient2'],
'Behold the wonderous power of email!',
{'X-Campaign-Id': 'something'},
function(err) { err && console.log(err) });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)])sendRaw('sender@example.com',
['recipient1@example.com', 'http://example.com/recipient2'],
'From: sender@example.com' +
'\nTo: ' + 'recipient1@example.com, 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) });Mailgun allows sender and recipient email addresses to be formatted in several different ways:
Mailgun understands a couple special headers, specified via options when using sendText, or in the MIME headers when using sendRaw. These are defined below.
Here's a complete sending example.
var Mailgun = require('mailgun').Mailgun;
var mg = new Mailgun('some-api-key');
mg.sendText('example@example.com', ['Recipient 1 <rec1@example.com>', 'rec2@example.com'],
'This is the subject',
'This is the text',
'noreply@example.com', {},
function(err) {
if (err) console.log('Oh noes: ' + err);
else console.log('Success');
});Mailgun lets you route incoming email to different destinations. TODO - more docs
Creates a new route. TODO - more docs
createRoute(pattern, destination, [callback(err, id)])TODO - document arguments
Deletes the route with the specified ID if it exists, otherwise fails silently.
deleteRoute(id, [callback(err)])Gets a list of all routes.
getRoutes(callback(err, routes))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);
}
});| Back | FazBrowse Home | New Git URL |