4

Basically, I want to parse all emails body from my gmail using Python. There are few solutions on StackOverflow e.g. the closest I found is here where he use poplib from Python to parse all emails. Another one is this one where he use .get_payload() to get email body

However, when I use parse email using poplib (as from above example) then each email message I do message.get_payload() to get email body, the string I get is not the same as html from email itself i.e. it has string like 3D, = \n, amp; etc. in my parsed email body.

I wonder if it's a problem with POP protocol so I need to use other protocol such as imaplib (but I don't know how to parse all email like poplib). Or, I can modify given example to get correct email body.

Community
  • 1
  • 1
titipata
  • 5,321
  • 3
  • 35
  • 59

1 Answers1

6

message.get_payload(decode=True) will be your best friend for this :)

According the docs:

Optional decode is a flag indicating whether the payload should be decoded or not, according to the Content-Transfer-Encoding header.

HTH

Todor Minakov
  • 19,097
  • 3
  • 55
  • 60