-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
97 lines (80 loc) · 2.35 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
'use strict'
const Promise = require('bluebird')
const nodemailer = require('nodemailer')
const mjml = require('mjml')
const glob = require('glob')
const fs = require('fs')
const path = require('path')
const set = require('lodash/set')
const trim = require('lodash/trim')
const Handlebars = require('handlebars')
// Global user config
let config = {}
// Cache template
const templates = {}
// nodemailer transport
let transport
const init = (userConfig) => {
config = userConfig
// Create cache for template (html, text and subject)
glob
.sync(path.join(config.directory, '/*.{mjml,html,text,subject}'))
.map((file) => {
const extension = path.extname(file)
const name = path.basename(file, extension)
let type = extension.replace(/^\./, '')
let content = trim(fs.readFileSync(file, 'utf8'))
if (type === 'mjml') {
type = 'html'
content = mjml(content).html
}
set(templates, [name, type], Handlebars.compile(content))
})
// Verify if all templates has html, text and subject
Object.keys(templates).map((name) => {
if (!templates[name].subject) {
throw new Error(`Mail template ${name} not has subject`)
}
if (!templates[name].html) {
throw new Error(`Mail template ${name} not has html version`)
}
if (!templates[name].text) {
throw new Error(`Mail template ${name} not has text version`)
}
})
// create nodemailer transport
transport = nodemailer.createTransport(config.nodemailer)
}
const send = (templateName) => {
// Verify if templateName exists
if (!templates[templateName]) {
throw new Error(
`The mail template ${templateName} is not found in : ${Object.keys(
templates
).join(', ')}`
)
}
const template = templates[templateName]
// Send mail
return (to, datas = {}, mailOptions = {}) => {
const mail = {
...config.mail, // global config for mail
to,
subject: template.subject(datas),
html: template.html(datas),
text: template.text(datas),
...mailOptions // specific config for this mail
}
return new Promise(function (resolve, reject) {
transport.sendMail(mail, (err, infos) => {
if (err) {
reject(err)
}
resolve(infos)
})
})
}
}
module.exports = send
module.exports.default = send
module.exports.init = init