-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmailer.js
63 lines (54 loc) · 1.89 KB
/
mailer.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
const nodemailer = require('nodemailer');
const https = require('https');
const handlebars = require('handlebars');
const fs = require('fs');
const autobind = require('auto-bind');
class Mailer {
constructor(webhookr) {
this._config = webhookr.config;
this._logger = webhookr.logger;
if (!!this._config.mailer) this._transport = nodemailer.createTransport(this._config.mailer);
autobind(this);
}
// -----------------------------------------------------
// Send email with optional template processing
// -----------------------------------------------------
send(options) {
if (!this._transport) return;
// Get the default values from the config file, if html and text
// templates are both specified, then
let cfgOpts = {};
if (!!this._config.mailer.templates) cfgOpts = this._config.mailer.templates[options.template] || {};
// Merge the config file and parameters passed, where the
// parameters passed take precedence over the config
const opts = Object.assign({}, cfgOpts, options);
// Load up the template(s) and populate the message
if (!!opts.textfile) {
const textContent = this._loadTemplate(opts.textfile);
const textTemplate = handlebars.compile(textContent);
opts.text = textTemplate(opts);
}
if (!!opts.htmlfile) {
const htmlContent = this._loadTemplate(opts.htmlfile);
const htmlTemplate = handlebars.compile(htmlContent);
opts.html = htmlTemplate(opts);
}
// Sendmail runs asynchronously (which is what we want), but
// the error handling cannot return the information back to
// the user when this happens. It can, however, go to the
// log file
return new Promise((resolve, reject) => {
this._transport.sendMail(opts, (error, data) => {
if (error) {
reject(error);
} else {
resolve(data);
}
});
});
}
_loadTemplate(filename) {
return fs.readFileSync(filename, 'utf-8');
}
}
module.exports = Mailer;