small fix to deal with finding email body. who knew?

workspace
Brendan Howell 4 years ago
parent 21b97bf189
commit cfd9be88d4

@ -102,10 +102,13 @@ class MailRoom(Bureau):
""" """
util tidies up message headers deals with encoding util tidies up message headers deals with encoding
""" """
#TODO: consider replacing this whole mess with
# https://pypi.python.org/pypi/mail-parser
internaldate = resp_obj[b'INTERNALDATE'] internaldate = resp_obj[b'INTERNALDATE']
msg_data = resp_obj[imap_id][b'RFC822'].decode('utf-8') msg_data = resp_obj[imap_id][b'RFC822'].decode('utf-8')
msg_obj = email.message_from_string(msg_data) msg_obj = email.message_from_string(msg_data,
policy=email.policy.default)
# format and tidy header data # format and tidy header data
msg = Message() msg = Message()
@ -120,21 +123,26 @@ class MailRoom(Bureau):
msg.attachments = [] # TODO: deal with attachments msg.attachments = [] # TODO: deal with attachments
# TODO: should use msg_obj.get_body and deal with HTML # TODO: should use msg_obj.get_body and deal with HTML
msg.content = "" msg.content = msg.get_body(('plain',))
if msg.content:
msg.content = msg.content.get_content()
else:
msg.content = "ERROR COULD NOT FIND EMAIL BODY!\n Big shrug."
# extract other sub-messages / attachments # extract other sub-messages / attachments
for part in msg_obj.walk(): #for part in msg_obj.walk():
# TODO: save interesting attachments to files # # TODO: save interesting attachments to files
# should clean these up on delete # # should clean these up on delete
part_content = "" # part_content = ""
if part.get_content_type() == "text/plain": # if part.get_content_type() == "text/plain":
part_content = part.get_content() # #part_content = part.get_content()
print("msg part content:", part_content) # part_content = part.get_content()
# part_content = msg.content.decode("utf-8") # print("msg part content:", part_content)
part_content = msg.content.replace("<", "&lt;") # # part_content = msg.content.decode("utf-8")
part_content = msg.content.replace(">", "&gt;") # part_content = msg.content.replace("<", "&lt;")
part_content = msg.content.replace("\n", "<br />") # part_content = msg.content.replace(">", "&gt;")
msg.content += part_content # part_content = msg.content.replace("\n", "<br />")
# msg.content += part_content
return msg return msg

Loading…
Cancel
Save