Home

Email Services library

An npm package whichprovides an abstraction between multiple email service providers.

If one of the services goes down, it failovers to another provider.

Install

npm install mavidser/fluffy-robot

Quick Start

const es = require('email-services');

// initialize providers with your credentials
const sendgrid = new es.SendGrid('username', 'password');
const mailgun = new es.Mailgun('username', 'password');

// create the email-service object using the providers you need
const emailService = new es.EmailService([
  sendgrid,
  mailgun
]);

// send the email
emailService.sendEmail({
  to: '[email protected]',
  from: '[email protected]',
  cc: '[email protected]',
  subject: 'Hi!',
  text: 'How you doin?',
  html: '<h1>How you doin?</h1>'
})
.then(res => {
  console.log(res);
})
.catch(err => {
  console.error(err.message);
});

Usage

Importing

const es = require('email-services');

Email Service Providers

Initialize a provider by suppying it the required credentials.

The supported providers are -

  • Sendgrid
  • Mailgun
  • Mandrill
  • Amazon SES
const ses = new es.SES('username', 'password', 'aws-region');

Combining the services

Create a new EmailService object by initializing it with an array of provider objects. More than one accounts for the same provider can be used too.

The failover is executed in the sequence defined by the array.

const emailService = new es.EmailService([
  sendgrid1,
  sendgrid2,
  ses,
  mailgun,
  mandrill1,
  mandrill2
]);

Sending the email

Use the sendEmail function of EmailService object to send the email.

emailService.sendEmail({
  to: '[email protected]',
  from: '[email protected]',
  subject: 'Hi!',
  text: 'How you doin?',
  html: '<h1>How you doin?</h1>'
})
.then(res => {
  console.log(res);
})
.catch(err => {
  console.error(err.message);
});

The providers can also directly be used for sending emails.

sendGrid.sendEmail({
  to: '[email protected]',
  from: '[email protected]',
  subject: 'Hi!',
  text: 'How you doin?',
})

Documentation

The module documentation can be found here.

License

MIT