forked from storj-archived/service-mailer
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathindex.js
More file actions
173 lines (173 loc) · 5.92 KB
/
index.js
File metadata and controls
173 lines (173 loc) · 5.92 KB
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.MailerBuilder = void 0;
var path_1 = __importDefault(require("path"));
var fs_1 = __importDefault(require("fs"));
var async_1 = __importDefault(require("async"));
var assert_1 = __importDefault(require("assert"));
var nodemailer_1 = __importDefault(require("nodemailer"));
var handlebars_1 = __importDefault(require("handlebars"));
var mail_1 = __importDefault(require("@sendgrid/mail"));
var Mailer = /** @class */ (function () {
function Mailer(options) {
options = options || {};
if (!options.sendgrid || !options.sendgrid.api_key) {
assert_1.default.ok(options.host, 'No SMTP host supplied');
assert_1.default.ok(options.port, 'No SMTP port supplied');
}
if (options.sendgrid && options.sendgrid.api_key) {
mail_1.default.setApiKey(options.sendgrid.api_key);
}
this.options = options;
this.transporter = nodemailer_1.default.createTransport(options);
}
/**
* Sends the email template to the specified address
* using SMTP protocol
* #dispatch
* @param {String} email
* @param {String} template
* @param {Object} context
* @param {Function} callback
*/
Mailer.prototype.dispatch = function (email, template, context, callback) {
var _this = this;
var done = callback || function () { };
this.getTemplate(template, function (err, template) {
if (err) {
return done(err);
}
var compiled = template(context);
var mailparams = {
to: email,
from: _this.options.from,
subject: compiled.subject,
html: compiled.markup,
text: compiled.plaintext
};
_this.transporter.sendMail(mailparams, done);
});
};
/**
* Sends the email template to the specified address,
* using SendGrid API
* #dispatch
* @param {String} email
* @param {String} template
* @param {Object} context
* @param {Function} callback
*/
Mailer.prototype.dispatchSendGrid = function (email, template, context, callback) {
var _this = this;
var done = callback || function () { };
if (!this.options || !this.options.sendgrid || !this.options.sendgrid.api_key) {
return callback(new Error('No SendGrid API Key provided'));
}
this.getTemplate(template, function (err, template) {
if (err) {
return done(err);
}
var compiled = template(context);
var mailparams = {
to: email,
from: _this.options.from,
subject: compiled.subject,
html: compiled.markup,
text: compiled.plaintext
};
mail_1.default.send(mailparams).then(function () {
callback();
}).catch(function (err) {
callback(err);
});
});
};
/**
* Loads and compiles the mail template
* #getTemplate
* @param {String} name
* @param {Function} callback
*/
Mailer.prototype.getTemplate = function (name, callback) {
assert_1.default.ok(name, 'No template name was supplied');
var stack = [
this.getSubject.bind(this, name),
this.getMarkup.bind(this, name),
this.getPlaintext.bind(this, name)
];
async_1.default.parallel(stack, function (err, results) {
if (err) {
return callback(err);
}
callback(null, function render(data) {
data = data || {};
return {
subject: results[0](data),
markup: results[1](data),
plaintext: results[2](data)
};
});
});
};
;
/**
* Helper for loading email template subject
* #_getSubject
* @param {String} name
* @param {Function} callback
*/
Mailer.prototype.getSubject = function (name, callback) {
this.getTemplateResource([[name, name].join('/'), 'subject'].join('.'), callback);
};
;
/**
* Helper for loading email template markup
* #_getMarkup
* @param {String} name
* @param {Function} callback
*/
Mailer.prototype.getMarkup = function (name, callback) {
this.getTemplateResource([[name, name].join('/'), 'html'].join('.'), callback);
};
;
/**
* Helper for loading email template plaintext
* #_getPlaintext
* @param {String} name
* @param {Function} callback
*/
Mailer.prototype.getPlaintext = function (name, callback) {
this.getTemplateResource([[name, name].join('/'), 'txt'].join('.'), callback);
};
;
/**
* Helper for loading email template resource
* #__getTemplateResource
* @param {String} filename
* @param {Function} callback
*/
Mailer.prototype.getTemplateResource = function (filename, callback) {
fs_1.default.exists(path_1.default.join(__dirname, 'templates', filename), function (exists) {
if (!exists) {
return callback(new Error('Resource "' + filename + '" does not exist'));
}
fs_1.default.readFile(path_1.default.join(__dirname, 'templates', filename), function (err, contents) {
if (err) {
return callback(err);
}
callback(null, handlebars_1.default.compile(contents.toString()));
});
});
};
;
return Mailer;
}());
function MailerBuilder(options) {
return new Mailer(options);
}
exports.MailerBuilder = MailerBuilder;
exports.default = Mailer;
module.exports = Mailer;