Mailman
Send emails in a comfortable way via models.
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 = ; // path to a folder where// your views are storedMailmanoptionsviewspath = 'path_to_views/'; // cache templates or notMailmanoptionsviewscache = false; // default template engine// guesses by extension otherwiseMailmanoptionsviewsdefault = 'ejs';
To setup a transport:
- Configuring one of the known services:
Mailman;
- Configuring default SMTP transport (options passed directly to nodemailer):
Mailman;
- Configuring with 3rd-party nodemailer transport:
// assuming that transport is// initialized nodemailer transport Mailman;
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)
Mailer // need to manually set mailer name // UserMailer => user { return 'user'; } // default from for all emails { return 'sender@sender.com'; } // default subject for all emails { return 'Hello World'; } // welcome email { // set all your template variables // on this thisfull_name = 'John Doe'; thiscurrentDate = ; } // forgot password email { thistoken = 12345; }
To send out each of these emails, simply:
var mail; mail = to: 'receiver@receiver.com' ; mail; mail = to: 'receiver@receiver.com' ; mail;
Tests
npm test
License
Mailman is released under the MIT License.