-
Notifications
You must be signed in to change notification settings - Fork 3
/
mailchimp.js
50 lines (44 loc) · 1.38 KB
/
mailchimp.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
const request = require('request');
var CONF = require('./config.json');
function mailchimpAddToList(email, language){
var subscriber = JSON.stringify({
"email_address": email,
"status": "subscribed",
"merge_fields": {
"LANGUAGE": language
}
});
return new Promise(function(resolve, reject) {
request({
method: 'POST',
url: CONF.mailchimp.url,
body: subscriber,
headers:{
Authorization: 'apikey '+ CONF.mailchimp.apiKey,
'Content-Type': 'application/json'
}
},
function(error, response, body){
if(error) {
console.log(error)
reject('error_occured');
}
else{
var bodyObj = JSON.parse(body);
let msg = '';
switch(bodyObj.status) {
case 400:
msg = 'already_subscribed';
break;
case 'subscribed':
msg = 'subscribed_successfully';
break;
default:
msg = 'error_occured'
}
resolve(msg);
}
});
});
}
module.exports.mailchimpAddToList = mailchimpAddToList;