Created
February 19, 2018 21:45
-
-
Save dvidsilva/160f4f2175c3d5365d2b8a8873358272 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const Config = require('../config'); | |
const Mailchimp = require('mailchimp-api-v3'); | |
const mailchimp = Config.MAILCHIMP_API_KEY ? new Mailchimp(Config.MAILCHIMP_API_KEY) : {}; | |
const addSubscriber = (email, data, update) => { | |
if (!email || !Config.MAILCHIMP_API_KEY || !Config.MAILCHIMP_LIST_ID) { | |
const msg = `Ignoring adding subscriber, missing params ${!email ? 'email': 'config'}`; | |
console.warn(msg); | |
return Promise.resolve({ msg }); | |
} | |
return mailchimp.post(`lists/${Config.MAILCHIMP_LIST_ID}`, { | |
update_existing: update !== undefined ? update : true, | |
members: [{ | |
email_address: email.toLowerCase(), | |
status : data.status || 'subscribed', | |
merge_fields: { | |
"FNAME": data.name, | |
}, | |
}], | |
}).then(m => { | |
if (m.errors) { | |
console.log('Error adding new subscriber to MC', m.errors); | |
} | |
return m; | |
}).catch(err => { | |
console.warn('Failed adding subscriber', email, err); | |
}); | |
} | |
module.exports = { | |
addSubscriber, | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment