mailman

0.3.3 • Public • Published

Mailman

Send emails in a comfortable way via models.

Circle CI

Features

  • Uses nodemailer under the hood to send out emails
  • All nodemailer transports (including 3rd-party ones) are supported
  • Uses consolidate.js to render email templates (views)
  • ActiveMailer-inspired API, very comfortable
  • Clean, simple and lightweight code base (162 sloc)

Installation

npm install mailman --save

Warning: Only Node.js v0.11.x and higher with --harmony enabled is required:

node --harmony something.js

Note: In order for the following examples to work, you need use something like co to run generators. Another note: If you want to use ES6 classes (like in the following examples), use babel. If not, there is an alternative API left from previous versions of Mailman.

Usage

Configuration

To configure Mailman's basic options:

var Mailman = require('mailman');
 
// path to a folder where
// your views are stored
Mailman.options.views.path = 'path_to_views/';
 
// cache templates or not
Mailman.options.views.cache = false;
 
// default template engine
// guesses by extension otherwise
Mailman.options.views.default = 'ejs';

To setup a transport:

Mailman.configure('gmail', {
    user: 'user@gmail.com',
    password: 'password'
});
  • Configuring default SMTP transport (options passed directly to nodemailer):
Mailman.configure({
   host: 'smtp.gmail.com',
   port: 465,
   secure: true,
   auth: {
       user: 'user@gmail.com',
       pass: 'password'
   }
});
  • Configuring with 3rd-party nodemailer transport:
// assuming that transport is
// initialized nodemailer transport
 
Mailman.configure(transport);

Views

Mailman uses consolidate.js to render many template engines easily. Mailman expects, that your folder with views is structured like this:

- user/
    - welcome.ejs
    - forgot_password.ejs
    - reset_password.ejs

In this folder structure, it is clear that User mailer has welcome, forgot_password and reset_password emails.

Defining Mailer and sending

To send out emails, you need to define mailer first. Mailer is a Class, that contains emails for certain entity. For example, User mailer may contain welcome, forgot password, reset password emails. Each of the emails is represented as a usual function, which should set template variables for a view.

Note: Email function name must be the same as its view name (camelCased)

class UserMailer extends Mailman.Mailer {
    // need to manually set mailer name
    // UserMailer => user
    get name () { return 'user'; }
 
    // default from for all emails
    get from () { return 'sender@sender.com'; }
 
    // default subject for all emails
    get subject () { return 'Hello World'; }
 
    // welcome email
    welcome () {
        // set all your template variables
        // on this
 
        this.full_name = 'John Doe';
        this.currentDate = new Date();
    }
 
    // forgot password email
    forgotPassword () {
        this.token = 12345;
    }
}

To send out each of these emails, simply:

var mail;
 
mail = new UserMailer({ to: 'receiver@receiver.com' }).welcome();
yield mail.deliver();
 
mail = new UserMailer({ to: 'receiver@receiver.com' }).forgotPassword();
yield mail.deliver();

Tests

npm test

License

Mailman is released under the MIT License.

Package Sidebar

Install

npm i mailman

Weekly Downloads

17

Version

0.3.3

License

MIT

Last publish

Collaborators

  • vdemedes