Skip to main content

In Python 3.x you can do it in a very easy way by importing 'imaplib' and 'email' packages. Although this is an older post but maybe my answer can help new comers on this post.

status, data = self.imap.fetch(num, '(RFC822)')  email_msg = email.message_from_bytes(data[0][1])   #email.message_from_string(data[0][1])  #If message is multi part we only want the text version of the body, this walks the message and gets the body.  if email_msg.is_multipart():   for part in email_msg.walk():   if part.get_content_type() == "text/plain":   body = part.get_payload(decode=True) #to control automatic email-style MIME decoding (e.g., Base64, uuencode, quoted-printable)   body = body.decode()    elif part.get_content_type() == "text/html":   continue  

Now you can print body variable and it will be in plaintext format :) If it is good enough for you then it would be nice to select it as accepted answer.

In Python 3.x you can do it in a very easy way by importing 'imaplib' and 'email' packages. Although this is an older post but maybe my answer can help new comers on this post.

status, data = self.imap.fetch(num, '(RFC822)')  email_msg = email.message_from_bytes(data[0][1]) #email.message_from_string(data[0][1])  #If message is multi part we only want the text version of the body, this walks the message and gets the body.  if email_msg.is_multipart():   for part in email_msg.walk():   if part.get_content_type() == "text/plain":   body = part.get_payload(decode=True) #to control automatic email-style MIME decoding (e.g., Base64, uuencode, quoted-printable)   body = body.decode()    elif part.get_content_type() == "text/html":   continue  

Now you can print body variable and it will be in plaintext format :) If it is good enough for you then it would be nice to select it as accepted answer.

In Python 3.x you can do it in a very easy way by importing 'imaplib' and 'email' packages. Although this is an older post but maybe my answer can help new comers on this post.

status, data = self.imap.fetch(num, '(RFC822)') email_msg = email.message_from_bytes(data[0][1])   #email.message_from_string(data[0][1]) #If message is multi part we only want the text version of the body, this walks the message and gets the body. if email_msg.is_multipart(): for part in email_msg.walk(): if part.get_content_type() == "text/plain": body = part.get_payload(decode=True) #to control automatic email-style MIME decoding (e.g., Base64, uuencode, quoted-printable) body = body.decode() elif part.get_content_type() == "text/html": continue 

Now you can print body variable and it will be in plaintext format :) If it is good enough for you then it would be nice to select it as accepted answer.

Source Link
Wahib Ul Haq
  • 4.4k
  • 3
  • 49
  • 45

In Python 3.x you can do it in a very easy way by importing 'imaplib' and 'email' packages. Although this is an older post but maybe my answer can help new comers on this post.

status, data = self.imap.fetch(num, '(RFC822)') email_msg = email.message_from_bytes(data[0][1]) #email.message_from_string(data[0][1]) #If message is multi part we only want the text version of the body, this walks the message and gets the body. if email_msg.is_multipart(): for part in email_msg.walk(): if part.get_content_type() == "text/plain": body = part.get_payload(decode=True) #to control automatic email-style MIME decoding (e.g., Base64, uuencode, quoted-printable) body = body.decode() elif part.get_content_type() == "text/html": continue 

Now you can print body variable and it will be in plaintext format :) If it is good enough for you then it would be nice to select it as accepted answer.