June 18, 2019

Extract Attachments From Email (mbox) using Python

Some days ago I had the idea to dig around in my mailbox and see what people have sent me over the years. As I was especially interested in the attachments (pictures) I googled a bit and found out you can quickly extract stuff from a mbox (used by a Dovecot SMTP server). Here’s what I cobbled together.

import mailbox
import dateutil.parser
from datetime import datetime
mbox = mailbox.mbox('/home/USER/mail/MAILBOX')

def extractattachements(message):
    if message.get_content_maintype() == 'multipart':
        for part in message.walk():
            if part.get_content_maintype() == 'multipart': continue
            if part.get('Content-Disposition') is None: continue
            fn = part.get_filename()
            frm = message['From']
            if fn is None: fn = "untitled"
            d = dateutil.parser.parse(message['Date'])
            pre = d.strftime("%Y-%m-%d-%H-%M-%S")
            filename = pre + "--" + frm + "--" +  fn
            print filename
            fb = open(filename,'wb')
            pl = part.get_payload(decode=True)
            if pl is None: break

for message in mbox:

© ho1ger 2015 - 2022