-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
218 lines (178 loc) · 5.9 KB
/
index.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
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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
/** rf-db-mailer
* @desc
* Fetch templates and translations from db, bulid and send mails.
* Uses "mustache" to compile html.
* into html and nodemailer for sending.
* @author felix furtmayr, [email protected], Rapidfacture GmbH
* @license ISC
*/
const mustache = require('mustache');
const nodemailer = require('nodemailer');
const htmlToText = require('html-to-text');
let log = require('rf-log').customPrefixLogger('[rf-db-template-mailer]');
let opts = {}; // options passed when creating an instance
let db = {};
let translations = []; // json tranlation files
let mailtemplates = [];
let subjects = [];
/** create an instance
*
* @example
* var mail = simpleTemplateMailer({
* defaultLanguage: 'en',
* transporter: { // nodemailer tramporter options
* host: 'smtp.test.mail.address',
* requiresAuth: false,
* },
* translationsPath: __dirname + "/translations",
* templatesPath: __dirname + "/templates",
* });
*
*/
module.exports = {
start,
reloadTemplates,
getTemplate,
send
};
function start (options, database) {
options = options || {};
db = database;
// housekeeping
if (!options.transporter) return log.error('no transporter defined, aborting');
// options passed when creating an instance
opts = {
defaultLanguage: options.defaultLanguage || 'de',
transporter: options.transporter,
translations: options.translations || {}
};
reloadTemplates();
return module.exports;
}
function reloadTemplates () {
// fetch from db
getTranslations();
getTemplates();
}
function getTranslations (callback) {
db.global.translations
.find({})
.exec(function (err, translationsList) {
if (err) {
log.error(err);
callback(err);
} else {
translationsList = translationsList || [];
translationsList.forEach(function (translation) {
translations[translation.lang] = translation.keys;
});
}
});
}
function getTemplates (callback) {
db.global.templates
.find({type: 'email'})
.exec(function (err, templates) {
if (err) {
log.error(err);
callback(err);
} else {
templates = templates || [];
templates.forEach(function (template) {
mailtemplates[template.name] = template.text;
subjects[template.name] = template.subject;
});
}
});
}
function getTemplate (templateOps, callback) {
// housekeeping
if (!callback) {
log.error('no callback defined');
return log.error('no callback defined');
}
if (!templateOps) {
log.error('no template defined');
return callback('no template defined');
}
let lang = templateOps.language || opts.defaultLanguage || 'de';
let translation = translations[lang];
let tmplName = templateOps.name;
let template = mailtemplates[tmplName];
// get a valid language
let langObj, message = {};
if (lang && translation) {
langObj = translation; // get choosen translation
} else if (translations && opts.defaultLanguage) {
log.info('no language found, switching to default');
langObj = translations[opts.defaultLanguage];
} else {
log.info('no language defined');
}
// add language extensions
// if (opts.translations[lang] && langObj) {
// for (var key in opts.translations[lang]) {
// langObj[key] = opts.translations[lang][key];
// }
// }
let compileData = {
data: templateOps.data,
lang: langObj,
templates: mailtemplates
};
// compile subject with mustache
if (subjects[tmplName]) {
message.subject = mustache.render(subjects[tmplName], compileData); // json inserted in "{{ }}"
// render twice: as the subject refernces to a translation which can also include translations/templates
message.subject = mustache.render(message.subject, compileData);
}
// compile html message with mustache
if (template) {
message.html = mustache.render(template, compileData); // json inserted in "{{ }}"
// render again if template references templates
message.html = mustache.render(message.html, compileData);
} else { // no template => return template subject
message.html = message.subject;
}
// generate plain text from html
if (!message.text && message.html) message.text = htmlToText.fromString(message.html, { wordwrap: 130 });
callback(null, message);
}
function send (template, message, callback) {
// housekeeping
let errMsg = '';
if (!template) errMsg = 'no template defined';
if (!message || !message.to) errMsg = 'no template and no options defined for nodemailer';
if (!callback) errMsg = 'no callback defined';
if (errMsg) {
log.error(errMsg);
return callback(errMsg);
}
let transporterOpts = JSON.parse(JSON.stringify(message.transporter || opts.transporter));
delete message.transporter; // don't forward critical data like
if (!transporterOpts) return log.error('no transporter defined, aborting');
getTemplate(template, function (err, mailContent) {
// housekeeping
if (err) {
log.error('error in getting template: ', err);
return callback(err);
}
message.subject = message.subject || mailContent.subject;
message.html = message.html || mailContent.html;
message.text = message.text || mailContent.text;
// send mail with nodemailer
// options: https://nodemailer.com/message/
nodemailer
.createTransport(transporterOpts)
.sendMail(message,
function (err, info) {
if (err) {
log.error('error in sendMail: ', err);
if (err.message) err = err.message;
} else {
log.success('successfull sent mail');
}
callback(err, info);
});
});
}