-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
72 lines (62 loc) · 1.63 KB
/
index.js
File metadata and controls
72 lines (62 loc) · 1.63 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
const Email = require('email-templates')
const Mailman = require('./mailman')
module.exports = function (options) {
return new Posto(options)
}
function Posto (options) {
this.options = options
}
Posto.prototype.send = function (data, send = true) {
const { fromAddress, fromName, sendEmail, templatePath, useMjml = false } = this.options
if (!fromAddress || !fromName || !templatePath) {
const error = new Error('You must pass a configuration object with properties fromAddress, fromName, sendEmail, templatePath')
throw error
}
const options = {
juice: true,
juiceResources: {
preserveImportant: true,
webResources: {
relativeTo: templatePath
}
},
message: {
from: `${fromName} <${fromAddress}>`
},
// When "send" is true (the default) mailman.js takes care of the appropriate transport for the current environment.
send,
preview: process.env.NODE_ENV === 'development' && sendEmail === false,
transport: Mailman(this.options),
views: {
root: templatePath,
options: {
extension: 'njk'
}
}
}
if (useMjml) {
options.render = (view, locals) => {
return require('./lib/mjml')(templatePath)(view, locals)
}
}
const email = new Email(options)
const { attachments, bcc, cc, to, template } = data
return email.send({
template,
message: {
attachments,
bcc,
cc,
to
},
locals: data
})
.then(response => {
return Promise.resolve(response)
})
.catch(err => {
console.log(err)
err.message = 'EMAIL_FAILED'
return Promise.reject(err)
})
}