nodemailer-templation

0.1.4 • Public • Published

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.">
*/

__templates__ _list of objects_ ```js var templates = { //name: "path" reply: path.resolve(__dirname, '../templates/reply.html'), answer: path.resolve(__dirname, '../templates/answer.html'), news-letter: path.resolve(__dirname, '../templates/news-letter.html') }; ```
__defaultTemplate__ _string_
The default template used when you don't send in a template string for the mailer options. ```js var defaultTemplate = path.resolve(__dirname, '../templates/default.html'); ```
__templateOptions__ _object_
[Lodash template options](https://lodash.com/docs#template). ```js var templateOptions = { interpolate: /{{([\s\S]+?)}}/g //use {{thing}} as a basis for dynamic message data }; ```
__generateTextFromHTML__ _boolean_
To generate text from HTML or neigh.

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'
  }
};

__from__ _string (required)_
The email address of where you are sending this from. You can either set this to be used for all templates, or send in a from field when you use the send() method below. ```js var from = "some@email.com"; ```

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)

Package Sidebar

Install

npm i nodemailer-templation

Weekly Downloads

0

Version

0.1.4

License

MIT

Last publish

Collaborators

  • hellsan631