2

I am using mailchimp3 in Python. I managed to make batch insertion of users using this function:

client = MailChimp(USERNAME, APIKEY)
def fill_list(list_id, subscribers_data):
    data = {'operations': create_subscriptions_data(list_id, subscribers_data)}
    client.batches.create(data)

def create_subscriptions_data(list_id, users_data):
    return [{'method': 'PUT',
             'path': 'lists/{}/members/{}'.format(list_id, str(md5(user['email_address'].encode('utf-8')))),
             'body': json.dumps(user)} for user in users_data]

Here is how one user dict looks like:

{"email_address": "user@somemail.com", "status": "subscribed"}

Then I wanted to use similar method to unsubscribe list of users. To achieve that I tried to use the same logic, just to change the user objects. Now, I used this user format:

{"email_address": "user@somemail.com", "status": "unsubscribed"}

It doesn't update the subscribe status. When I deleted all users manually (using the web interface) and tried the same command I successfully created users with "status": "unsubscribed". I am wondering why this approach can't change the status? I tried also using POST instead of PUT, but it didn't work. Any idea what can be the issue?

I used this reference https://devs.mailchimp.com/blog/batch-operations-and-put-in-api-v3-0/ and it mentions that this approach should work fine for updates as well.

Thank you in advance!

giliev
  • 2,938
  • 4
  • 27
  • 47

2 Answers2

4

The only way to unsubscribe an already subscribed user will be to update with a list id and an MD5 hash of the lowercase version of the list member’s email address.

client.lists.members.update('LIST_ID', 'MD5 HASH', {"status": "unsubscribed"})

1

Actually, I was using some wrong functions, so here is the fixed code. I also had some problems with the size of the batches. The maximum batch size is 500, so I did some splits of the data across several batches. Here is a simple code how the insertion should be done:

client = MailChimp(USERNAME, APIKEY)

def _update_list(list_id: str, members_data: list):
    client.lists.update_members(list_id, {'members': members_data, 'update_existing': True})

Each member in members_data has data like this:

mailchimp_user = {
        'email_address': user.email,
        'status': user.subscription_status,
        'merge_fields': {
            'FNAME': user.first_name,
            'LNAME': user.last_name
        },
        'interests': {}
    }

And here comes the most important function:

def fill_in_multiple_batches(list_id, mailchimp_members):
    step_size = 400

    for i in range(0, len(mailchimp_members), step_size):
        batch_start_idx = i
        batch_end_idx = min(i + step_size, len(mailchimp_members))
        this_batch_of_members = mailchimp_members[batch_start_idx:batch_end_idx]

        client.lists.update_members(list_id, {'members': members_data, 'update_existing': True})

After that, in the main of the script:

if __name__ == '__main__':
    fill_in_multiple_batches('your_list_id', your_data_list)
giliev
  • 2,938
  • 4
  • 27
  • 47