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 storedMailman.options.views.path = 'path_to_views/';// cache templates or notMailman.options.views.cache = false;// default template engine// guesses by extension otherwiseMailman.options.views.default = '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
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();