Create emails using Derby JS apps/templates. It uses derby-render to render views and juice to inline styles.
For convenience, the default results returned match the values read by nodemailer.
$ npm install derby-email --save
Create your views:
index.html
<import: src="./other">
<From:>
{{unescaped $formatEmail('app', 'foo@bar.com')}}
<Body:>
<p>Some text.</p>
<Text:>
Some text.
Any view named after a field and capitalized will be returned as a result.
other.html
<Subject:>
Hello {{user}}
<Body:>
<p>foo bar</p>
<Text:>
foo bar
Send your email:
var derby = require('derby');
var app = derby.createApp('app', __filename);
var email = require('derby-email')(app);
var nodemailer = require('nodemailer');
app.loadViews(...);
app.loadStyles(...);
function send(err, emailOptions) {
var transporter = nodemailer.createTransports(...);
transporter.send(emailOptions, function (err, info) {
...
})
};
// return email options
email(send);
// with data
var data = {_page: {userId: '...'}};
email(data, send);
// with a specific page (namespace)
email('welcome', send);
// or with both
email('welcome', data, send);
The following view functions are available in your views:
$formatEmail(name, address) – Returns a formatted email address. i.e. name <foo@bar.com>
.
All options are also passed in to derby-render. See derby-render for a list of options.
fields – The fields (views) to render and return. Includes: html, text, subject, from, to, etc. See Nodemailer for a list of suggested fields.
css – Configuration options passed to inline-css. See [inline-css]https://github.com/jonkemp/inline-css).