A wrapper around Nodemailer used for sending email. It can be used with or without a template engine.
npm install hapi-mailer
The plugin accepts the following configuration options:
transport
: A Nodemailer transport mechanism. If it is not setnodemailer-direct-transport
transport is used. If it is a regular objectnodemailer-smtp-transport
is used and the value is passed as SMTP configuration.views
: The views configuration as described in the server'sviews
option. Note that due to the way noderequire()
operates, plugins must require rendering engines directly and pass the engine using theengines.module
option. Note that relative paths are relative to the plugin root, not the working directory or the application registering the plugin.
Example:
var Handlebars = require('handlebars');
var options = {
transport: {
service: 'Gmail',
auth: {
user: '[email protected]',
pass: 'password'
}
},
views: {
engines: {
html: {
module: Handlebars.create(),
path: Path.join(__dirname, 'lib/views/emails')
}
}
}
};
var plugin = {
register: require('hapi-mailer'),
options: options
};
server.register(plugin, function (err) {
...
});
In handlers, the Mailer
object can be accessed as request.server.plugins.mailer
. It has a sendMail
function which can be used to send an email. It accepts the following configuration options:
data
: Defines the mail content the same way as Nodemailer. There is only one additional propertycontext
, which is an optional object used by the template to render context-specific result.callback
: It is a callback function to run once the message is delivered or it failed.
Example:
var handler = function (request, reply) {
var data = {
from: '[email protected]',
to: '[email protected]',
subject: 'Example Subject',
html: {
path: 'handlebars.html'
},
context: {
name: 'Example User'
}
};
var Mailer = request.server.plugins.mailer;
Mailer.sendMail(data, function (err, info) {
reply();
});
};
server.route({ method: 'POST', path: '/', handler: handler });