Search code examples
pythongoogle-app-engineemailpython-2.7email-attachments

How to get decode attachment filename with python email?


I use the following code to extract filename of the attachment:

import email.utils

msg = email.message_from_string(self.request.body) # http://docs.python.org/2/library/email.parser.html
for part in msg.walk():
    ctype = part.get_content_type()
    if ctype in ['image/jpeg', 'image/png']:
        image_file = part.get_payload(decode=True)
        image_file_name = part.get_filename()

It works well in many cases, but sometime as image_file_name I get values like =?KOI8-R?B?xsHTLTk2Mi5qcGc=?= or =?UTF-8?B?REkyeTFXMFNMNzAuanBn?=.

How should I handle such cases?


Solution

  • You should look at the three parts separated by '?', and use the first two as instructions for how to treat the third:

    The first bit is the character-encoding (KO18-R and UTF-8 in your examples), and the second bit is a 'B' to indicate base64 encoding - Q in it's place would indicate quoted-printable, so you should prepare your code for that as well.