Skip to content

Instantly share code, notes, and snippets.

@dvidsilva
Created February 19, 2018 21:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dvidsilva/160f4f2175c3d5365d2b8a8873358272 to your computer and use it in GitHub Desktop.
Save dvidsilva/160f4f2175c3d5365d2b8a8873358272 to your computer and use it in GitHub Desktop.
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