forked from forwardemail/email-templates
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
206 lines (184 loc) · 6.71 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
const path = require('path');
const debug = require('debug')('email-templates');
const fs = require('fs-extra');
const htmlToText = require('html-to-text');
const I18N = require('@ladjs/i18n');
const autoBind = require('auto-bind');
const nodemailer = require('nodemailer');
const consolidate = require('consolidate');
const isFunction = require('lodash.isfunction');
const isObject = require('lodash.isobject');
const isEmpty = require('lodash.isempty');
const isString = require('lodash.isstring');
const defaultsDeep = require('lodash.defaultsdeep');
const merge = require('lodash.merge');
const previewEmail = require('preview-email');
const getPaths = require('get-paths');
const juiceResources = require('juice-resources-promise');
class Email {
constructor(config = {}) {
debug('config passed %O', config);
this.config = merge(
{
views: {
// directory where email templates reside
root: path.resolve('emails'),
options: {
// default file extension for template
extension: 'pug',
map: {},
engineSource: consolidate
},
// locals to pass to templates for rendering
locals: {
// pretty is automatically set to `false` for subject/text
pretty: true
}
},
// <https://nodemailer.com/message/>
message: {},
send: !['development', 'test'].includes(process.env.NODE_ENV),
preview: process.env.NODE_ENV === 'development',
// <https://github.com/ladjs/i18n>
// set to an object to configure and enable it
i18n: false,
// pass a custom render function if necessary
render: this.render.bind(this),
// <https://github.com/werk85/node-html-to-text>
htmlToText: {
ignoreImage: true
},
// <https://github.com/Automattic/juice>
juice: true,
juiceResources: {
preserveImportant: true,
webResources: {
relativeTo: path.resolve('build')
}
},
// pass a transport configuration object or a transport instance
// (e.g. an instance is created via `nodemailer.createTransport`)
// <https://nodemailer.com/transports/>
transport: {}
},
config
);
if (!isObject(this.config.transport) || isEmpty(this.config.transport))
throw new Error(
'Transport option must be a transport instance or configuration object'
);
if (!isFunction(this.config.transport.sendMail))
this.config.transport = nodemailer.createTransport(this.config.transport);
debug('transformed config %O', this.config);
autoBind(this);
}
// promise version of consolidate's render
// inspired by koa-views and re-uses the same config
// <https://github.com/queckezz/koa-views>
render(view, locals) {
return new Promise(async (resolve, reject) => {
try {
const { map, engineSource, extension } = this.config.views.options;
const paths = await getPaths(this.config.views.root, view, extension);
const filePath = path.resolve(this.config.views.root, paths.rel);
const suffix = paths.ext;
if (suffix === 'html' && !map) {
const res = await fs.readFile(filePath, 'utf8');
resolve(res);
} else {
const engineName = map && map[suffix] ? map[suffix] : suffix;
const render = engineSource[engineName];
if (!engineName || !render)
return reject(
new Error(`Engine not found for the ".${suffix}" file extension`)
);
// TODO: convert this to a promise based version
render(filePath, locals, (err, res) => {
if (err) return reject(err);
resolve(res);
});
}
} catch (err) {
reject(err);
}
});
}
send(options = {}) {
options = Object.assign(
{
template: '',
message: {},
locals: {}
},
options
);
let { template, message, locals } = options;
message = defaultsDeep(this.config.message, message);
locals = defaultsDeep(this.config.views.locals, locals);
debug('template %s', template);
debug('message %O', message);
debug('locals (keys only): %O', Object.keys(locals));
return new Promise(async (resolve, reject) => {
try {
if (isObject(this.config.i18n)) {
const i18n = new I18N(
Object.assign({}, this.config.i18n, {
register: locals
})
);
// support `locals.user.last_locale`
// (e.g. for <https://lad.js.org>)
if (isObject(locals.user) && isString(locals.user.last_locale))
locals.locale = locals.user.last_locale;
if (isString(locals.locale)) i18n.setLocale(locals.locale);
}
if (!message.subject && template)
message.subject = await this.config.render(
`${template}/subject`,
Object.assign({}, locals, { pretty: false })
);
if (!message.html && template)
message.html = await this.config.render(`${template}/html`, locals);
if (!this.config.htmlToText && !message.text && template)
message.text = await this.config.render(
`${template}/text`,
Object.assign({}, locals, { pretty: false })
);
else if (this.config.htmlToText && message.html)
// we'd use nodemailer-html-to-text plugin
// but we really don't need to support cid
// <https://github.com/andris9/nodemailer-html-to-text>
message.text = htmlToText.fromString(
message.html,
this.config.htmlToText
);
// transform the html with juice using remote paths
// google now supports media queries
// https://developers.google.com/gmail/design/reference/supported_css
if (this.config.juice && message.html)
message.html = await juiceResources(
message.html,
this.config.juiceResources
);
if (this.config.preview) {
debug('using `preview-email` to preview email');
await previewEmail(message);
}
if (!this.config.send) {
debug('send disabled so we are ensuring JSONTransport');
// <https://github.com/nodemailer/nodemailer/issues/798>
// if (this.config.transport.name !== 'JSONTransport')
this.config.transport = nodemailer.createTransport({
jsonTransport: true
});
}
const res = await this.config.transport.sendMail(message);
debug('message sent');
resolve(res);
} catch (err) {
reject(err);
}
});
}
}
module.exports = Email;