i would like to programmatically access via python all the mail that exists on a Mac OS X system, which is received by the built in "Mail.app" program. are there friendly apis for accessing the mail as stored by that program? My impression is that it's not just a text format and that it might be more complicated. thanks.
As of 2020, you can use the Python emlx library.
pip install emlx
Example code:
import emlx
import glob
for filepath in glob.iglob("/Users/<username>/Library/Mail/**/*.emlx", recursive=True):
m = emlx.read(filepath)
On a message m
, you can do various operations:
>>> m.headers
{'Subject': 'Re: Emlx library ✉️',
'From': 'Michael <[email protected]>',
'Date': 'Thu, 30 Jan 2020 20:25:43 +0100',
'Content-Type': 'text/plain; charset=utf-8',
...}
>>> m.headers['Subject']
'Re: Emlx library ✉️'
>>> m.plist
{'color': '000000',
'conversation-id': 12345,
'date-last-viewed': 1580423184,
'flags': {...}
...}
>>> m.flags
{'read': True, 'answered': True, 'attachment_count': 2}
If you need speed, you can parse the plist
and flags
only:
>>> m = emlx.read(filepath, plist_only=True)
Mail.app stores messages as .emlx files, which is an undocumented format AFAIK. But you could convert .emlx files to the standard mbox format (using this) and then process them with the mailbox module.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With