read content from mailbox file
This commit is contained in:
parent
0ac2f2e9c1
commit
81cdb61101
|
@ -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))
|
||||
|
Loading…
Reference in New Issue