0

I am trying to read content from Gmail using poplib. The content in my email is mostly base64 encoded but contains some additional symbols. However, when I read the content using poplib, for some reason my original content is base64 encoded again.

Example content in my email: {{{eyJjb250ZW50IjpbeyJjZWxsIjoiQTEiLCJ2YW

Example response that I get from poplib: e3t7ZXlKamIyNTBaVzUwSWpwYmV5SmpaV3hzSWpvaVFURWlMQ0oyWVd

Any suggestions on how to retrieve my original (raw) content (without poplib encoding it again) will be greatly appreciated.

Here is the code that I am using:

messages = [pop_conn.retr(i) for i in range(1, len(pop_conn.list()[1]) + 1)]
psr = parser.FeedParser()
for msg in messages:
    psr.feed(str(msg))
    mess = psr.close()
payload = mess.get_payload(decode=True) 
user3603634
  • 95
  • 2
  • 11
  • Are you sure it isn't just the mime encoding for the message? What mime headers are there on the message? – Martijn Pieters Nov 22 '15 at 15:53
  • From what I can see these are the headers: 'MIME-Version: 1.0', 'Content-Type: text/plain; charset=ISO-8859-1; format=flowed; delsp=yes', 'Content-Transfer-Encoding: base64', '', I can see that 'Content-Transfer-Encoding' is 'base64' , but I am not sure how I can remove that or change it. Any suggestions? – user3603634 Nov 22 '15 at 16:04
  • Use the [`email` library](https://docs.python.org/2/library/email.html), it includes transparent content-transfer-encoding handling. – Martijn Pieters Nov 22 '15 at 16:06
  • I *suspect* that the messages are perhaps already `email.Message` objects? Not sure what the `parser.FeedParser()` object is and why you need to use `psr.feed()` there with a `str(msg)` result. – Martijn Pieters Nov 22 '15 at 16:08
  • Yes, I am using it, I have imported the parser in my code from the email library, i.e. `from email import parser` and then `psr = parser.FeedParser()` – user3603634 Nov 22 '15 at 16:10
  • But why the `str(msg)` call then? – Martijn Pieters Nov 22 '15 at 16:10
  • The [`POP.retr()` method](https://docs.python.org/2/library/poplib.html#poplib.POP3.retr) returns a *tuple*, where the object at index 1 contains the message lines. Feeding in the whole tuple with the first and last parts is probably not a good idea. – Martijn Pieters Nov 22 '15 at 16:12
  • My guess is that `poplib` returns exactly what's there, and that something in the *sending* code encodes it behind your back. If you examine the message source in your inbox, what do you see? – tripleee Nov 22 '15 at 16:35

0 Answers0