I am trying to read the content of the email. If an email contains an image in the BODY part I need to extract and save it in the local Here is the code for getting the body content
def get_body(id):
res, mail_data = connection.fetch(id, '(RFC822)')
raw_email=mail_data[0][1]
email_message_instance = email.message_from_string(raw_email)
whole_body = ''
for part in email_message_instance.walk():
if part.get_content_type() == "text/plain": # ignore attachments/html
body = part.get_payload(decode=True)
whole_body += body + ' '
else:
continue
return whole_body
Found the solution.
def get_body(id):
"""Get the body of the email"""
res, mail_data = connection.fetch(id, '(RFC822)')
raw_email=mail_data[0][1]
email_message_instance = email.message_from_string(raw_email)
whole_body = ''
for part in email_message_instance.walk():
if part.get_content_type() == "text/plain": # ignore attachments/html
body = part.get_payload(decode=True)
print "body" + body
whole_body += body + ' '
if part.get_content_maintype() != 'multipart' and part.get('Content-Disposition') is not None:
print "image content"
image = part.get_filename().split('.')
image_name = image[0] + id + "." + image[1]
open(E: + '/' + image_name, 'wb').write(part.get_payload(decode=True))
else:
continue
return whole_body
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With