Python Zugriff auf Thunderbird

Literatur

import mailbox
from email.header import decode_header, make_header
import os
from glob import glob


def isDeleted(msg):
    flags = msg['X-Mozilla-Status']
    try:
        flags = int(flags, 16)
        if (flags & 8) != 0:
            return True
        else:
            return False
    except:
        return False


def getSubject(aSubject):
    return (make_header(decode_header(aSubject)))


def isUnread(msg):
    flags = msg['X-Mozilla-Status']
    try:
        flags = int(flags, 16)
        if (flags & 1) == 0:
            return True
        else:
            return False
    except:
        return False


def listMails(aMailbox, i):
    for message in mailbox.mbox(aMailbox):
        subject = message['subject']  # Could possibly be None.
        if subject:
            if not isDeleted(message):
                if isUnread(message):
                    i += 1
                    print(i, ' ', getSubject(subject))
    return i


def allInboxes(aPath):
    i = 0
    aInboxes = [y for x in os.walk(aPath) for y in glob(os.path.join(x[0], '*/Inbox'))]
    for aInbox in aInboxes:
        i = listMails(aInbox, i)


if __name__ == '__main__':
    aPfad = os.path.join(os.path.expanduser('~'), '.thunderbird')
    allInboxes(aPfad)