File: //proc/1233/root/home/arjun/projects/buyercall/buyercall/blueprints/email/utils/email_parser.py
from email import policy
from email.parser import BytesParser
class NotSupportedMailFormat(Exception):
pass
class EmailParser:
""" """
msg = ''
def parse_attachments(self):
msg = self.msg
# TODO get the attachments from email
return []
def parse(self, content):
"""
Parse the email and return a dictionary of relevant data.
"""
subject = html = plain = from_ = to = ''
attachments = []
try:
content = content['Body'].read()
self.msg = BytesParser(policy=policy.SMTP).parsebytes(content)
subject = self.msg['Subject']
# get the plain text version of the email
try:
plain = self.msg.get_body(preferencelist='plain')
plain = ''.join(plain.get_content().splitlines(keepends=True))
plain = '' if plain is None else plain
except Exception:
print('Incoming message does not have an plain text part - skipping this part.')
# get the HTML version of the email
try:
html = self.msg.get_body(preferencelist='html')
html = ''.join(html.get_content().splitlines(keepends=True))
html = '' if html is None else html
except Exception:
print('Incoming message does not have an HTML part - skipping this part.')
attachments = self.parse_attachments()
from_ = self.msg.get('From', '')
to = self.msg.get('To', '')
except Exception as e:
print('Error : ', e)
return {
'subject': subject,
'plain': plain,
'from': from_,
'to': to,
'html': html,
'attachments': attachments,
}