Skip to content
Advertisement

Read email in python 3.7 using imaplib with HTML body and attachments in the email

I would really appreciate if someone can help me with this issue.

I have implemented the below code to read “unread emails from gmail inbox”. I need to print “To”, “From”, “Subject”, “Body” and “save attachments in a specified location”

I have 2 issues here.

  1. If there is any email with attachments, it gives the error Body: [<email.message.Message object at 0x026D1050>, <email.message.Message object at 0x02776B70>]. It will print all the required things and saves attachments but DOESN’T print the body.

This works fine if no attachment is included.

  1. If there is an email body with any styling in it like “bold/italic/underline/colour…etc”, it doesn’t print as it is.

Example : Python is printed as Python=C2=A0i= and sometimes different styling is seperated by “*”.

JavaScript

Many thanks

Advertisement

Answer

I new to python and this is the complete working code I have done to read unseen emails. You can print the elements according to your requirements. It works for gmail and office 365. This script runs for every 10 seconds. This might also work for other email providers by passing the credentials. Hope this helps.

JavaScript
User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement