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

@danfebooks/sails-service-mailer

v3.3.0

Published

Service for Sails framework with Mailer feautres

Downloads

34

Readme

sails-service-mailer

Email Service for Sails framework

npm version

List of supported mail transports

  • Direct (sends email directly to MX server)
  • SendGrid (SendGrid API)
  • Sendmail (sends email via sendmail)
  • Amazon SES (sends email via Amazon SES services)
  • SMTP (sends email via some of SMTP servers)
  • Stub (stub sending of email)

Getting Started

Install this module.

npm install @danfebooks/sails-service-mailer

Then import it in your service and create mailer instance.

// api/services/MailerService.js
import MailerService from '@danfebooks/sails-service-mailer';

// Note: If you are using commonjs `require`, then please use
const MailerService = require('@danfebooks/sails-service-mailer').default;


export default MailerService('sendmail', {
  from: '[email protected]',
  subject: 'Hello, there',
  provider: {
    path: '/usr/bin/sendmail'
  }
});

// api/controllers/MailController.js
export default {
  send: function(req, res) {
    MailerService
      .send({
        to: req.param('to'),
        text: 'And of course, Hello World!'
      })
      .then(res.ok)
      .catch(res.negotiate);
  }
};

Configuration

There is two kind of configuration - provider configuration and mail configuration.

When you instantiate new instance of mailer, in configuration object you can add provider object. This object will send directly to one of nodemailer transports.

And all keys that don't belongs to provider will send directly to sendMail function.

So basic configuration can be:

let mailer = MailerService('direct', {
  from: '[email protected]' // this will go to sendMail,
  provider: { // this will go to nodemailer.createTransport
    name: 'some.mx-server.com'
  }
});

Each of available options you can find in nodemailer transport repositories or a little bit below in examples.

  • Mail options you can find here.
  • Provider options you can find in appropriate repository of nodemailer transports.

API

Each of Mailer instances has only one method:

send(config)

config - Configuration object with mail options like from, to, etc... It will mix up to the pre-defined config. All allowed options for this object you can find here.

Returns Promise.

Examples

All of this examples contains all the provider configuration keys. And most of them is optional.

DirectMailer

let direct = MailerService('direct', {
  from: '[email protected]',
  provider: {
    name: '<MX_HOSTNAME>', // hostname to be used when introducing the client to the MX server
    debug: false // if true, the connection emits all traffic between client and server as `log` events
  }
});

SendGridMailer

let sendGrid = MailerService('sendgrid', {
  from: '[email protected]',
  provider: {
    auth: {
      api_key: '<SENDGRID_APIKEY>' // SendGrid API KEY
    }
  }
});

SendMailMailer

let sendmail = MailerService('sendmail', {
  from: '[email protected]',
  provider: {
    path: '/usr/bin/sendmail', // path to the sendmail command
    args: [] // an array of extra command line options to pass to the `sendmail` command
  }
});

SESMailer

let ses = MailerService('ses', {
  from: '[email protected]',
  provider: {
    ses: {}, // instantiated AWS SES object with new AWS.SES()
    accessKeyId: 'MY_KEY', // AWS access key
    secretAccessKey: 'MY_SECRET', // AWS secret key
    sessionToken: '', // Session token
    region: '', // Specify the region to send the service request
    httpOptions: {}, // A hash of options to pass to the low-level AWS HTTP request
    rateLimit: 5 // Specify the amount of messages can be sent in 1 second
  }
});

SMTPMailer

let smtp = MailerService('smtp', {
  from: '[email protected]',
  provider: {
    port: 25, // The port to connect to
    host: 'localhost', // The hostname to connect to
    secure: false, // Defines if the connection should use SSL
    auth: { // Defines authentication data
      user: '', // Username
      pass: '', // Password
      xoauth2: '' // OAuth2 access token
    },
    ignoreTLS: false, // Turns off STARTTLS support if true
    name: '', // Options hostname of the client
    localAddress: '', // Local interface to bind to for network connections
    connectionTimeout: 2000, // How many ms to wait for the connection to establish
    greetingTimeout: 2000, // How many ms to wait for the greeting after connection
    socketTimeout: 2000, // How many ms of inactivity to allow
    debug: false, // If true, the connection emits all traffic between client and server as `log` events
    authMethod: 'PLAIN', // Defines preferred authentication method
    tls: {} // Defines additional options to be passed to the socket constructor
  }
});

StubMailer

let stub = MailerService('stub', {
  from: '[email protected]',
  provider: {
    error: new Error('Invalid recipient') // If you want that sending will fail and return error
  }
});