Package Exports
- mailgun-js
This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (mailgun-js) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.
Readme
mailgun.js
Simple Node.js module for Mailgun.
Installation
npm install mailgun-js
Usage overview
Please see Mailgun Documentation for full Mailgun API reference. Depends on request module.
Most methods take a data
parameter, which is a Javascript object that would contain the arguments for the Mailgun API.
All methods take a final parameter callback with three parameters: error
, response
, and body
, exactly like the request callback.
We try to parse the body
into a javascript object, and return it to the callback as such for easier use and inspection by the client.
response.statusCode
will be 200
if everything worked OK. See Mailgun documentation for other (error) response codes.
If there was an error a new Error
object will be passed to the callback in the error
parameter.
Currently we only implement the send message
(non-MIME) API and the Mailboxes
, Routes
, and Mailing Lists
API's. These would be the most common
and practical API's to be programmatically used. Others would be easy to add if needed.
var api_key = 'key-XXXXXXXXXXXXXXXXXXXXXXX';
var domain = 'mydomain.mailgun.org';
var mailgun = require('mailgun-js')(api_key, domain);
var data = {
from: 'Excited User <me@samples.mailgun.org>',
to: 'serobnic@mail.ru',
subject: 'Hello',
text: 'Testing some Mailgun awesomness!'
};
mailgun.messages.send(data, function (error, response, body) {
console.log(body);
});
API
All methods take a callback as their last parameter. The callback is called with a Javascript Error
(if any) and then the response
and the body
returned by mailgun.
For actual examples see the tests source code. Note that routes
and lists
API's do not act on specified mailgun domains and are global for the mailgun account.
mailgun.messages
- Creates a new email message and sends it using mailgun..send(data)
- send a message.
mailgun.mailboxes
- create, update, delete and list mailboxes..list(data)
- list mailboxes.data
is optional and can containlimit
andskip
..create(data)
- create a mailbox.data
should havemailbox
name andpassword
..update(data)
- update a mailbox given themailbox
name. Currently only thepassword
can be changed..del(mailbox)
- delete a mailbox given themailbox
name.
mailgun.routes
- create, get, update, delete and list routes..list(data)
- list routes.data
is optional and can containlimit
andskip
..get(id)
- get a specific route given the routeid
..create(data)
- create a route.data
should containpriority
,description
,expression
andaction
as strings..update(id, data)
- update a route given routeid
. Alldata
parameters optional. This API call only updates the specified fields leaving others unchanged..del(id)
- delete a route given routeid
.
mailgun.lists
- create, get, update, delete and list mailing lists and get mailing list stats..list(data)
- list mailing lists.data
is optional and can containaddress
,limit
andskip
..get(address)
- get a specific mailing list given mailing listaddress
..create(data)
- create a mailing list.data
should containaddress
,name
,description
, andaccess_level
as strings..update(address, data)
- update a mailing list given mailing listaddress
..del(address)
- delete a mailing list given mailing listaddress
..stats(address)
- fetches mailing list stats given mailing listaddress
.
mailgun.lists.members
- create, get, update, delete and list mailing list members..list(listAddress, data)
- list mailing list members.data
is optional and can containsubscribed
,limit
andskip
..get(listAddress, memberAddress)
- get a specific mailing list member given mailing list address and member address..create(listAddress, data)
- create a mailing list member.data
should containaddress
, optional membername
,subscribed
,upsert
, and any additionalvars
..update(listAddress, memberAddress, data)
- update a mailing list member with given properties. Won't touch the property if it's not passed in..del(listAddress, memberAddress)
- delete a mailing list member given mailing list address and member address.
Tests
To run the test suite you must first have a Mailgun account with a domain setup. Then create a file named ./test/auth.json, which contains your credentials as JSON, for example:
{ "api_key": "key-XXXXXXXXXXXXXXXXXXXXXXX", "domain": "mydomain.mailgun.org" }
You should edit ./test/fixture.json and modify at least the to
and from
fields of the message
object to match
emails you would like to test with. Modify other fields as desired, though the given defaults will work.
Then install the dev dependencies and execute the test suite:
$ npm install
$ npm test
The tests will call Mailgun API, and will send a test email, create mailbox(es), route(s), mailing list and mailing list member.
TODO
- Other API sections.
License
This project is not endorsed by or affiliated with Mailgun.
Copyright 2012, 2013 OneLobby
Licensed under the MIT License.