Counting Messages in IMAP Folders in Python

Continuing on my last post (), I wanted to show a snippet of Python code that will list a set of IMAP folders and count the number of messages in them. You give it a starting folder and it lists that folder plus all/any sub-folders and their message counts. At the very end it prints a total message count.

Listing IMAP Mailboxes with Python

I’ve been working on some Python scripts recently to manage my IMAP mailboxes and the information I found on the internet was fairly good, but lacked a completeness that my newbie Python skills required. So, I figured I’d post a complete example to list mailboxes (folders) for your IMAP account. This was developed using Python 3.3.

I hope this code snippet help you on your way to Python IMAP goodness. If not, it will at least server as a reminder to myself the next time I need to do something like this.