如何处理=?windows-1251?Q?在邮件附件?

时间:2014-07-30 17:28:23

标签: python google-app-engine email python-2.7 email-attachments

我收到电子邮件附件并将其直接保存到blobstore:

msg = email.message_from_string(self.request.body)
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()
    if image_file_name[:11] == '=?KOI8-R?B?':
      image_file_name = base64.b64decode(image_file_name[11:]).decode('KOI8-R')    
    if image_file_name[:10] == '=?UTF-8?B?':
      image_file_name = base64.b64decode(image_file_name[10:])

如何处理文件名中的Q值?在我问的另一个question中,有人说Q表示引用可打印。但是我该怎么做以获得有效的文件名?

UPD 即可。我用

替换了代码
filename_decoded = ''
for item in decode_header(filename):
    if item[1]:
        filename_decoded += item[0].decode(item[1])
    else:
        filename_decoded += item[0]
if filename_decoded != '':
    filename = filename_decoded

它帮助我=?windows-1251?Q?,但却没有正常的名字 - 'ascii' codec can't encode characters in position 11-14: ordinal not in range(128)。我该如何解决这个问题?

0 个答案:

没有答案