-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
60 lines (55 loc) · 1.3 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
const axios = require('axios')
const FormData = require('form-data')
const formatEmailToFormData = (from, replyTo, to, bcc, subject, text, html) => {
const emailData = new FormData()
emailData.append('from', from)
emailData.append('to', to)
if (replyTo) {
emailData.append('replyTo', replyTo)
}
if (bcc) {
emailData.append('bcc', bcc)
}
if (subject) {
emailData.append('subject', subject)
}
if (text) {
emailData.append('text', text)
}
if (html) {
emailData.append('html', html)
}
return emailData
}
module.exports = {
init: (providerOptions = {}, settings = {}) => {
return {
send: async options => {
const { apiKey, baseURL } = providerOptions
const {
from = settings.defaultFrom,
replyTo = settings.defaultReplyTo,
to, bcc, subject, text, html
} = options
const emailData = formatEmailToFormData(
from,
replyTo,
to,
bcc,
subject,
text,
html
)
await axios.request({
url: `https://${baseURL}/email/3/send`,
method: 'POST',
headers: {
Authorization: `App ${apiKey}`,
...emailData.getHeaders()
},
data: emailData
})
}
}
}
}