-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
135 lines (115 loc) · 3.77 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
'use strict';
var axios = require('axios');
function throwError(err) {
throw new Error(err);
}
module.exports.send_via_sendgrid = function(params) {
var data = {
'personalizations': [],
'from': {
'email': params.from_email,
'name': params.from_name
},
'content': [
{
'type': 'text/html',
'value': params.content
}
]
};
if (params.hasOwnProperty('groups')) {
if (typeof(params.groups) != 'object') {
throwError('Invalid typeof "groups" in params. Must be array of objects.');
return
}
params.groups.forEach(group => {
var personalizations = {};
var send_to = [];
var send_cc = [];
var send_bcc = [];
if (!group.hasOwnProperty('to')) {
throwError('Missing "to" in "groups" params.');
return
}
if (typeof(group.to) == 'object') {
group.to.forEach(email => {
send_to.push({ email });
});
} else {
send_to.push({ 'email': group.to });
}
personalizations.to = send_to;
if (group.hasOwnProperty('cc')) {
if (typeof(group.cc) == 'object') {
group.cc.forEach(email => {
send_cc.push({ email });
});
} else {
send_cc.push({ 'email': group.cc });
}
personalizations.cc = send_cc;
}
if (group.hasOwnProperty('bcc')) {
if (typeof(group.bcc) == 'object') {
group.bcc.forEach(email => {
send_bcc.push({ email });
});
} else {
send_bcc.push({ 'email': group.bcc });
}
personalizations.bcc = send_bcc;
}
personalizations.subject = group.subject;
data['personalizations'].push(personalizations);
});
} else {
var personalizations = {};
var send_to = [];
var send_cc = [];
var send_bcc = [];
if (!params.hasOwnProperty('to')) {
throwError('Missing "to" in params.');
return
}
if (typeof(params.to) == 'object') {
params.to.forEach(email => {
send_to.push({ email });
});
} else {
send_to.push({ 'email': params.to });
}
personalizations.to = send_to;
if (params.hasOwnProperty('cc')) {
if (typeof(params.cc) == 'object') {
params.cc.forEach(email => {
send_cc.push({ email });
});
} else {
send_cc.push({ 'email': params.cc });
}
personalizations.cc = send_cc;
}
if (params.hasOwnProperty('bcc')) {
if (typeof(params.bcc) == 'object') {
params.bcc.forEach(email => {
send_bcc.push({ email });
});
} else {
send_bcc.push({ 'email': params.bcc });
}
personalizations.bcc = send_bcc;
}
personalizations.subject = params.subject;
data['personalizations'].push(personalizations);
}
return axios({
method: 'POST',
url: 'https://api.sendgrid.com/v3/mail/send',
headers: {
'Authorization': 'Bearer ' + params.sendgrid_key,
'Content-Type': 'application/json',
'Access-Control-Allow-Origin': '*'
},
data: JSON.stringify(data)
});
}