❮ Table of contents

Basic Python 3 IMAP client

Python 3 has excellent support for IMAP via its standard library, making it easy to download mails from any server supporting the protocol. The code below will download mails in chunks and yield them as JSON to the console as they come in, and meant to be piped into a tool like jq. Two external libraries are used: dacite (MIT license), which turns dicts into dataclasses, and markdownify (MIT license) for turning html emails into markdown.

Note that this is taylored to my specific use case, where only the latest email in a thread of emails are needed, which usually contains previous emails in a conversation. The way it's implemented means that branched/non linear conversations probably won't be handled correctly, and some may be discarded. Simply remove the code doing this if you want all emails.

Requirements

pip install dacite markdownify