user3599280 user3599280 - 2 months ago 6x
Python Question

Store email locally (imaplib, libpst)

I'm using

to fetch emails for several accounts (Gmail, Yahoo..).
What is the best way to store emails locally (including attachments).

Is there any way to
and store emails as file?
Is it possible to store emails as bytes and retrieve them
later as mail object?

I'll try to save mail in separate folder with each field in JSON file
and attachment as separate files, but I was wondering if there is a
way of doing it.


There are already several established ways to store mailboxes (i.e. a list of emails). Popular examples are Maildir and mbox.

Python includes the mailbox module which can handle them:

Supported mailbox formats are Maildir, mbox, MH, Babyl, and MMDF.

You can of course roll your own solution, pickle them or dump them as JSON to a file, but if you use one of the common formats, you gain compatibility with other programs (importing them into Thunderbird, for example).