Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

API for programmatically accessing mail in Python on mac os X

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.


2 Answers

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)
like image 124
Michael Avatar answered Sep 15 '25 17:09

Michael


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.

like image 26
Óscar López Avatar answered Sep 15 '25 19:09

Óscar López