nodemailer-strategies

Use multiple email providers, or fall back to disk backups, when using nodemailer. NS avoids providers with high latency intelligently and can also redirect all email to console or disk when testing and debugging.

npm install nodemailer-strategies
4 downloads in the last month

Nodemailer Strategies

Nodemailer Strategies will let you customize your own email strategies, to give you better control if one email providers goes down, or you run your code in test.

With the in-built strategy blocks you can build a strategy that will switch between Gmail and SendGrid, and if both go down, it will fall back to disk. and print the emails to console or you can build a completely different one with suits you better.

When you are testing your application, just replace your production stragy with a Console strategy or a fallback strategy, and you wont have to touch the rest of your email code.

Usage

var nodemailer = require("nodemailer");
var ns = require("nodemailer-strategies");

var googleTransport = nodemailer.createTransport("SMTP",{
    service: "Gmail",
    auth: {
        user: "gmail.user@gmail.com",
        pass: "userpass"
    }
});
var sendGridTransport = nodemailer.createTransport("SMTP",{
    service: "SendGrid",
    auth: {
        user: "apiUser",
        pass: "apiKey"
    }
});

var emailStrategy = new ns([
        {name: "lower-latency", options:[
            {name: "nodemailer", options: googleTransport},
            {name: "nodemailer", options: sendGridTransport}
        ]},
        {name: "fallback", options:{ path: "./queuedEmails/"}}
]) 

var mailOptions = {
    from: "Fred Foo ✔ <foo@blurdybloop.com>", // sender address
    to: "bar@blurdybloop.com, baz@blurdybloop.com", // list of receivers
    subject: "Hello ✔", // Subject line
    text: "Hello world ✔", // plaintext body
    html: "<b>Hello world ✔</b>" // html body
}

emailStrategy.sendMail(mailOptions, function(err, success){
    console.log("Executed the strategy with success? %s", success)
})

Strategy blocks

Fallback to disk

{name:"delay", options:{path: "path to storage directory"}}

Lower Latency

Will use x number of other stragies (usually nodemailer strategies) and use statistics to select the best performing one, and switch favorites if performance degrades.

{name:"lower-latency", options:{providers:[ strategy blocks... ]}}

Console

This strategy will always "fail" and thus proceede to the next strategy in the line.

{name:"console"}

Delay

{name:"delay", options:{time: 1000}}

Nodemailer

{name: "nodemailer", options: nodemailerTransport}

Background

This library was built to ensure that long running node processes can automatically send emails even though one or several of the email providers become offline. Nodemailer Strategies will try email providers in the order the user has determined. It can also use a latency aware wrapper around several email providers to ensure that the most responsive provider is used, and that unstable providers are quickly ignored.

This design was chosen after SendGrid was victim of a DDoS attack and it became apparent that many webapplications have resilient architecture when it comes to application servers, databases and networking equipment, but not e-mail providers.

Contributing

All contributions are welcome. To get your provider accepted, you need to supply tests and notify the maintainers of any needed credentials to test your provider.

npm loves you