Templating

Nodemailer allows to use simple built-in templating or alternatively external renderers for common message types.

var transporter = nodemailer.createTransport(...);
var send = transporter.templateSender(templates, [defaults]);

// send a message based on provided templates
send(mailData, context, callback);
// or
send(mailData, context).then(...).catch(...);

Where

  • templates is an object with template strings for built-in renderer or an EmailTemplate object for more complex rendering
// built-in renderer
var send = transporter.templateSender({
    subject: 'This template is used for the "subject" field',
    text: 'This template is used for the "text" field',
    html: 'This template is used for the "html" field'
});
// external renderer
var EmailTemplate = require('email-templates').EmailTemplate;
var send = transporter.templateSender(new EmailTemplate('template/directory'));
  • defaults is an optional object of message data fields that are set for every message sent using this sender
  • mailData includes message fields for current message
  • context is an object with template replacements, where key replaces {{key}} when using the built-in renderer
var templates = {
    text: 'Hello {{username}}!'
};
var context = {
    username: 'User Name'
};
// results in "Hello, User Name!" as the text body
// of the message when using built-in renderer
  • callback is the transporter.sendMail callback (if not set then the function returns a Promise)

NB! If using built-in renderer then template variables are HTML escaped for the html field but kept as is for other fields

Example 1. Built-in renderer

var transporter = nodemailer.createTransport('smtps://user%40gmail.com:[email protected]');

// create template based sender function
var sendPwdReminder = transporter.templateSender({
    subject: 'Password reminder for {{username}}!',
    text: 'Hello, {{username}}, Your password is: {{ password }}',
    html: '<b>Hello, <strong>{{username}}</strong>, Your password is:\n<b>{{ password }}</b></p>'
}, {
    from: [email protected]',
});

// use template based sender to send a message
sendPwdReminder({
    to: [email protected]'
}, {
    username: 'Node Mailer',
    password: '!"\'<>&some-thing'
}, function(err, info){
    if(err){
       console.log('Error');
    }else{
        console.log('Password reminder sent');
    }
});

Example 2. External renderer

var EmailTemplate = require('email-templates').EmailTemplate;
var transporter = nodemailer.createTransport('smtps://user%40gmail.com:[email protected]');

// create template based sender function
// assumes text.{ext} and html.{ext} in template/directory
var sendPwdReminder = transporter.templateSender(new EmailTemplate('template/directory'), {
    from: [email protected]',
});

// use template based sender to send a message
sendPwdReminder({
    to: [email protected]',
    // EmailTemplate renders html and text but no subject so we need to
    // set it manually either here or in the defaults section of templateSender()
    subject: 'Password reminder'
}, {
    username: 'Node Mailer',
    password: '!"\'<>&some-thing'
}, function(err, info){
    if(err){
       console.log('Error');
    }else{
        console.log('Password reminder sent');
    }
});

Custom renderer

In addition to the built-in and node-email-templates based renderers you can also bring your own.

var sendPwdReminder = transporter.templateSender({
    render: function(context, callback){
        callback(null, {
            html: 'rendered html content',
            text: 'rendered text content'
        });
    }
});

Example. Using swig-email-templates

var EmailTemplates = require('swig-email-templates');
var transporter = nodemailer.createTransport('smtps://user%40gmail.com:[email protected]');

// create template renderer
var templates = new EmailTemplates();

// provide custom rendering function
var sendPwdReminder = transporter.templateSender({
    render: function(context, callback){
        templates.render('pwreminder.html', context, function (err, html, text) {
            if(err){
                return callback(err);
            }
            callback(null, {
                html: html,
                text: text
            });
        });
    }
});
...