5

I download messages from a Gmail account using POP3 and save them in a SQLite database for futher processing:

mailbox = poplib.POP3_SSL('pop.gmail.com', '995') 
mailbox.user(user) 
mailbox.pass_(password)

msgnum = mailbox.stat()[0]

for i in range(msgnum):
    msg = '\n'.join(mailbox.retr(i+1)[1])
    save_message(msg, dbmgr)

mailbox.quit()

However, looking in the database, all lines but the last one of the message body (payload) have trailing equal signs. Do you know why this happens?

John Manak
  • 13,328
  • 29
  • 78
  • 119
  • 4
    Possible duplicate of [How to understand the equal sign '=' symbol in IMAP email text?](http://stackoverflow.com/questions/15621510/how-to-understand-the-equal-sign-symbol-in-imap-email-text) – Frédéric Hamidi Dec 11 '14 at 11:16

2 Answers2

4

Frederic's link lead me to the answer. The encoding is called "quoted printable" (wiki) and it's possible to decode it using the quopri Python module (documentation):

msg.decode('quopri').decode('utf-8')
John Manak
  • 13,328
  • 29
  • 78
  • 119
1

Update for python 3.x

You now have to invoke the codecs module.

import codecs
bytes_msg = bytes(msg, 'utf-8')
decoded_msg = codecs.decode(bytes_msg, 'quopri').decode('utf-8')
George Griffin
  • 634
  • 7
  • 17