I learned something today. In sitting down to finish up my mailchimp functions, I wanted to create a mailchimp_remove_teacher_from_list(listname) function and a mailchimp_blanket_unsubscibe_teacher() function. I thought that, after my previous success, it would go quickly.
I was wrong.
Over here, in the docs, there’s reference to how to delete list members, which is what I basically want to do:
I knew what a list_id was, but wasn’t sure what the subscriber_hash was. After a bit of Googleing (I thought I’d have to ask mailchimp for the hash) I found this page and was able to piece it together:
In previous versions of the API, we exposed internal database IDs
leidfor emails and list/email combinations. In API 3.0, we no longer use or expose either of these IDs. Instead, we identify your subscribers by the MD5 hash of the lowercase version of their email address so you can easily predict the API URL of a subscriber’s data.
For example, to get the MD5 hash of the email address Urist.McVankab@freddiesjokes.com, first convert the address to its lowercase version: firstname.lastname@example.org. The MD5 hash of email@example.com is
In short, the subscriber_hash argument is something you produce at your end and use to look a subscriber up. There’s something elegant about it — you have no business accessing the subscriber if you don’t know the email address, already — but I needed some time to get it done.
Here’s how I did it:
import hashlib # A standard library that does hashes # The following is in .lower() case because mailchimp forms # hashes from lowercase strings. # The .encode() method tagged on the end encodes it as a byte literal email = request.user.email.lower().encode(encoding='utf-8) # This uses the hashlib library to make the hash. The .hexdigest() # seems to be about equivalent to str() [forgive me internet!] hash = hashlib.md5(email).hexdigest() client.lists.members.delete(list_id=list_id, subscriber_hash=hash)
This is not a complete solution. It basically tags onto my last post. At some point, I want to compend everything I do into something more universally applicable. When I do, I have no plans to keep it secret.