Python Mailjet API Example


Here’s a way to get all unsubscribed in your account ever;

I’m having trouble with the fact it starts blocking me after a few “bad requests” trying to remove a contact that’s not in the list, but oh well. This is progress.

def get_all_unsubscribed():
	import mailjet

	api = mailjet.Api()
	list_ids = [x['id'] for x in api.lists.all()['lists']]

	unsubscribed = []

	for list_id in list_ids:
		total_accounted_for = 0
		total_count = api.lists.Contacts(id=list_id, status='unsub')['total_cnt']
		while total_accounted_for < total_count:
			if total_accounted_for:
				result = api.lists.Contacts(id=list_id, status='unsub', start=total_accounted_for)
				result = api.lists.Contacts(id=list_id, status='unsub')
			print 'found ... results ', len(result['result'])
			if not result['result']:
				print 'found end... @ ', total_accounted_for, total_count
			for u in result['result']:
			total_accounted_for += len(result['result'])

	return unsubscribed

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s