Quantcast
Channel: Active questions tagged javascript - Stack Overflow
Viewing all articles
Browse latest Browse all 142382

Unsubscribe on Mailchimp with a Google App Script API

$
0
0

I adapted the following google app script code to unsubscribe an email from a list on MailChimp. Similar code, allows me to add a subscriber but when I try to modify a member's details or change the status to unsubscribed. I get an error:

"title":"Member Exists","status":400,"detail":"example@example.com is already a list member. Use PUT to insert or update list members."

Eventhough I am using "PUT". Any suggestions?

function sendToMailChimp_( email, source ) {

  var user = {
    "SRC" : source
  };

  var payload = {
"merge_fields" : user,
    "email_address": email,
    "status" : "unsubscribed",
    "update_existing" : true // left the update_existing on this way existing emails 
  };

  var headers = {
    "content-type" : "application/json",
    "Authorization" : " Basic " + Utilities.base64Encode(API_USER + ":" + API_KEY)
  };
 Logger.log(headers);
  var options = {
    "method": "Put",
    "headers" : headers,
    "payload": JSON.stringify(payload),
 "followRedirects" : true,
  "muteHttpExceptions": true,
  };

  Logger.log(mc_base_url);
  Logger.log( options );
  //var options = JSON.stringify( options ));
  var emailHash = xxxxxxxxxxxxxxxxxxxxxxxxxx // (email in md5 format); 
var listId = xxxxxxxx // (list id from Mailchimp)
  var response = UrlFetchApp.fetch('https://us2.api.mailchimp.com/3.0/lists/'+listId+'/members/'+emailHash, options );
  if( response.getResponseCode() == 200 ) {
    console.info( "SUCCESS to PROD for " + em );
    Logger.log( "SUCCESS to PROD for " + em );
  } else {
    console.log(response);
    Logger.log(response)
}
}

Viewing all articles
Browse latest Browse all 142382

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>