PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Tuesday, September 6, 2022

[FIXED] How to unsubscribe / delete list members using Mailchimp3 in Python?

 September 06, 2022     mailchimp-api-v3.0, python     No comments   

Issue

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!


Solution

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)


Answered By - giliev
Answer Checked By - Marie Seifert (PHPFixing Admin)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing