I want to extract only the body of a message and return it. I can filter on the fields and display the snippet but not the body.
def GetMimeMessage(service, user
Try this:
mail
param is your mime_msg
variable
def get_mpart(mail):
maintype = mail.get_content_maintype()
if maintype == 'multipart':
for part in mail.get_payload():
# This includes mail body AND text file attachments.
if part.get_content_maintype() == 'text':
return part.get_payload()
# No text at all. This is also happens
return ""
elif maintype == 'text':
return mail.get_payload()
def get_mail_body(mail):
"""
There is no 'body' tag in mail, so separate function.
:param mail: Message object
:return: Body content
"""
body = ""
if mail.is_multipart():
# This does not work.
# for part in mail.get_payload():
# body += part.get_payload()
body = get_mpart(mail)
else:
body = mail.get_payload()
return body