read content from mailbox file

This commit is contained in:
Rob 2019-07-30 14:32:09 +02:00
parent 0ac2f2e9c1
commit 81cdb61101
1 changed files with 49 additions and 0 deletions

49
src/Mail/thunderbird.py Normal file
View File

@ -0,0 +1,49 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import sys
import os
import mailbox
import email.header
import email.message
if 2 != len(sys.argv):
print("Error. Please povide your mailbox file (mbox) as parameter with the script!")
exit()
mboxfile = sys.argv[1]
'''
Email MIME based content converting
https://stackoverflow.com/a/36716138
'''
def decode_mime_words(s):
return u''.join(
word.decode(encoding or 'utf8') if isinstance(word, bytes) else word
for word, encoding in email.header.decode_header(s))
'''
Get content of email
https://stackoverflow.com/a/31489271
'''
def getbody(message): #getting plain text 'email body'
body = None
if message.is_multipart():
for part in message.walk():
if part.is_multipart():
for subpart in part.walk():
if subpart.get_content_type() == 'text/plain':
body = subpart.get_payload(decode=True)
elif part.get_content_type() == 'text/plain':
body = part.get_payload(decode=True)
elif message.get_content_type() == 'text/plain':
body = message.get_payload(decode=True)
return body
mbox = mailbox.mbox(mboxfile)
for message in mbox:
mFrom = decode_mime_words(message['from'])
mSubject = decode_mime_words(message['subject'])
print( getbody(message))