-
Notifications
You must be signed in to change notification settings - Fork 0
/
mail.js
82 lines (69 loc) · 2.01 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
var config = require('./config/config.js');
var sg = require('sendgrid')(config['production'].sendgrid);
var sgListId = config['production'].sendgridListId;
module.exports = {
setUp: function(to,subject,textType,content){
var type = 'text/plain';
if (textType == 'html'){
type = 'text/html';
}
var helper = require('sendgrid').mail;
var fromEmail = new helper.Email('info@communitycred.com','Community Cred');
var toEmail = new helper.Email(to);
var subject = subject;
var content = new helper.Content(type, content);
var mail = new helper.Mail(fromEmail, subject, toEmail, content);
return mail.toJSON();
},
send: function(mailObj){
var request = sg.emptyRequest({
method: 'POST',
path: '/v3/mail/send',
body: mailObj
});
return sg.API(request);
},
addUser: function(email){
var request = sg.emptyRequest();
request.body = [
{
"email": email
}
];
request.method = 'POST';
request.path = '/v3/contactdb/recipients';
return sg.API(request);
},
addRecipientToList: function(listId,recipientId){
var request = sg.emptyRequest();
request.method = 'POST';
request.path = '/v3/contactdb/lists/' + listId + '/recipients/' + recipientId;
return sg.API(request);
},
addEmailToUserList: function(email){
return this.addUser(email)
.then(r=>{
if (r.body.persisted_recipients[0]){
var recipientId = r.body.persisted_recipients[0];
return this.addRecipientToList(sgListId,recipientId);
} else {
throw new Error("Email could not be added");
return false;
}
})
}
};
/*
// With promise
sg.API(request)
.then(function (response) {
console.log(response.statusCode);
console.log(response.body);
console.log(response.headers);
})
.catch(function (error) {
// error is an instance of SendGridError
// The full response is attached to error.response
console.log(error.response.statusCode);
});
*/