0.1.4 • Published 9 years ago

nodemailer-templation v0.1.4

Weekly downloads
5
License
MIT
Repository
github
Last release
9 years ago

nodemailer-templation

A Nodemailer wrapper for easily sending HTML templated emails.

I've always had problems trying to send emails via multiple HTML templates. There really wasn't anything that solved all of my needs in a way that was easy and made sense to me. I found a code snippit some where for node 0.8x, that didn't work, so I adapted it into this package.

Installation

Install via Download,

NPM (recommended)

npm install --save nodemailer-templation

Useage

You need an external SMTP server or service to make this work. I recommend Mandrill. Its free, and fast.

Example using mandrill as our SMTP service.

var Templation  = require('nodemailer-templation');
var path        = require('path');

//Create our new new mailer object
var Mailer = new Templation({
    from: 'hello@example.com',
    templates: {
      reply: path.resolve(__dirname, '../templates/reply.html')
    },
    attachments: [
      {
        filename: 'logoLite.png',
        path: path.resolve(__dirname, '../templates/images/logoLite.png'),
        cid: 'light@logo'
      },
      {
        filename: 'logoDark.png',
        path: path.resolve(__dirname, '../templates/images/logoDark.png'),
        cid: 'dark@logo'
      }
    ],
    transportOptions: {
      host: 'smtp.mandrillapp.com',
      port: 587,
      auth: {
        user: 'SomeUserName',
        pass: 'SomeUserNameAPIKEY'
      }
    }
  });

//Send a mail using a template you've created, and listed under the templates option above.
Mailer.send({
  to: 'acoolguy@google.com',
  subject: 'Hello World',
  template: 'reply',
  messageData: {
    title: 'Hello Dude',
    name: 'Woah',
    message: 'Far Out'
  }
});

//Send a mail using the default template
Mailer.send({
  to: 'a2coolguy@google.com',
  subject: 'Hello World',
  messageData: {
    title: 'Hello Dude',
    name: 'Woah',
    message: 'Far Out',
    copymark: '(c) TooCool LLC 1995'
  }
});

Templation Options

There are the options you can send in when you create your new Templation object.

attachments (array) of objects

var attachments = [
  {
    filename: 'logoLite.png',
    path: path.resolve(__dirname, '../templates/images/logoLite.png'),
    cid: 'light@logo' //used in your template when you send emails.
  }
];

/* Example including above logo attachment
<img src="cid:light@logo" alt="My Company LLC.">
*/

transportOptions object (required) nodemailer-smtp-transport options. You can either set this to be used for all templates, or send in a transportOptions field when you use the send() method below.

var transportOptions = {
  host: 'smtp.mandrillapp.com',
  port: 587,
  auth: {
    user: 'SomeUserName',
    pass: 'SomeUserNameAPIKEY'
  }
};

Templation.send() Method Options

These are the options when you send using your Template object

var Mailer = new Templation(templationOptions);
Mailer.send(mailerOptions);

to object or string (required)

var to = { //equivalent to "Bruce Wayne <iamnotbatman@wayne.com>"
  name: 'Bruce Wayne',
  email: 'iamnotbatman@wayne.com'
};

//OR

var to = "iamnotbatman@wayne.com";

from string (see above)

subject string (required)

var subject = 'TPS Reports';

template string The name of the template if you defined one above, or a (string) path to a template.html file

var template = 'report';

//OR

var template = path.resolve(__dirname, '/someTemplate.html');

messageData object Corresponding fields to what is inside of the template.html file

var messageData = {
  title: 'Hello Dude',
  name: 'Woah',
  message: 'Far Out',
  copymark: '(c) TooCool LLC 1995'
};

transportOptions object (see above)