-
Notifications
You must be signed in to change notification settings - Fork 16
/
mail.js
239 lines (200 loc) · 5.37 KB
/
mail.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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
/* Copyright (c) 2013-2020 Richard Rodger and other contributors, MIT License */
'use strict'
const Uuid = require('uuid')
const Email = require('email-templates')
module.exports = mail
module.exports.defaults = ({ Joi }) => ({
test: Joi.boolean()
.default(false)
.description('Test mode.'),
history: Joi.boolean()
.default(true)
.description('Save mail history to `sys/mailhist` entity.'),
// NOTE: Joi function default must be returned from a default maker function
makehist: Joi.function()
.default(() => () => {})
.description('Add properties to `sys/mailhist` entity.'),
logmail: Joi.boolean()
.default(true)
.description('Log mail sending at info level.'),
// options for nodemailer
email: Joi.object({
// NOTE: for safety the default is to not send email
send: Joi.boolean()
.default(false)
.description('Send email (for safety, off by default).'),
preview: Joi.boolean()
.default(false)
.description('Preview email.')
})
.unknown()
.default()
.description('Options for nodemailer.')
})
function mail(options) {
var seneca = this
var mailer
// TODO seneca 4.x with embedded promisify should support this
seneca.depends('promisify')
seneca
.message('sys:mail,send:mail', send_mail)
.message('sys:mail,hook:render', hook_render)
seneca.prepare(async function() {
var root = seneca.root
var email_opts = options.email
// create transport
var transport_opts = email_opts.transport
if (transport_opts) {
var transport_maker = require('nodemailer-' + transport_opts.name)
var transport = transport_maker(transport_opts.options)
email_opts.transport = transport
}
email_opts.render = async (view, content) => {
var orig_code = view.split('/')[0]
var part = view.split('/')[1]
var code = null
var owner = null
var orbit = null
if (orig_code.includes('~')) {
var subcodes = orig_code.split('~')
code = subcodes[0]
owner = subcodes[1]
orbit = subcodes[2]
} else {
code = orig_code
}
// empty string is an artefact of code format
owner = '' === owner ? null : owner
// TODO: how to make this action specific?
var res = await root.post('sys:mail,hook:render', {
code,
owner,
orbit,
part,
content,
merge: content.__merge__
})
if (null == res) {
return null
}
if (false === res.ok) {
throw new Error('mail-render-failed: ' + (res ? res.why : 'unknown'))
}
var out = res[part]
if ('html' === part) {
out = await mailer.juiceResources(out)
}
return out
}
mailer = new Email(email_opts)
})
async function send_mail(msg, meta) {
// NOTE: rendering and merging are performed by implementing hook:render yourself
var content = msg.content // content to merge
var merge = msg.merge || {} // merge rules
// Template format is code~owner~orbit
var template = msg.code
if (null != msg.owner) {
template = template + '~' + msg.owner
}
if (null != msg.orbit) {
template =
template +
// NOTE: empty owner may be needed
(null != msg.owner ? '' : '~') +
'~' +
msg.orbit
}
// TODO: support domain suffix?
var messageId = Uuid.v4()
var locals = {
...content,
__merge__: merge
}
var mail_opts = {
template,
message: {
to: msg.to,
from: msg.from,
subject: msg.subject,
messageId
},
locals
}
var sent = await mailer.send(mail_opts)
// NOTE: avoid sending internal objects back (sendgrid issue)
var sentdesc = Array.isArray(sent)
? {
message:
sent.originalMessage && sent.originalMessage.html
? sent.originalMessage.html
: ''
}
: sent
var statusCode = sent
? sent.statusCode
? sent.statusCode
: sent[0]
? sent[0].statusCode
: 0
: 0
var result =
sent && 'function' === typeof sent.toJSON
? sent.toJSON()
: sent[0] && 'function' === typeof sent[0].toJSON
? sent[0].toJSON()
: {
messageId: messageId,
statusCode: statusCode
}
var savehist =
(options.history && false !== msg.history) ||
(!options.history && true === msg.history)
var when = Date.now()
if (savehist) {
// do not wait
seneca
.entity('sys/mailhist')
.data$({
...msg,
template,
when,
mid: messageId,
status: statusCode,
sent: sentdesc,
result,
...options.makehist({ msg, meta, template, sent, result, when })
})
.save$()
}
if (options.logmail) {
seneca.log.info({
notice: 'email-sent',
data: {
...msg,
template,
when,
mid: messageId,
status: statusCode
}
})
}
return {
msg,
sent: sentdesc,
result,
template,
mid: messageId,
status: statusCode
}
}
async function hook_render(msg) {
return {
html:
'NO RENDER DEFINED FOR ' +
msg.code +
', content was: ' +
JSON.stringify(msg.content)
}
}
}