I'm trying to make a program that will get the content of an email, my current code is as follows:
def get_message_content(_msg):
def get_section(msg):
payload = msg.get_payload()
if type(payload) is list:
for part in payload:
for _content in get_section(part):
yield _content
else:
content_type = msg.get_content_type()
if content_type == 'text/plain':
if msg['Content-Transfer-Encoding'] == 'base64':
yield b64decode(payload).decode('utf-8')
else:
yield payload
return ''.join(content for content in get_section(_msg))
Where _msg is an email object
This works fine when the email is encoded in base64 or ASCII, but if the email is encoded in quoted-printable then it doesn't work properly (i get lots of = in my output). I was unable to use quopri.decodestring
to decode quoted printable, and cant seem to get msg.get_payload(decode=True)
to work either.
How can I resolve this? Thanks