如何使用python电子邮件获取解码附件文件? [英] How to get decode attachment filename with python email?

查看:1168
本文介绍了如何使用python电子邮件获取解码附件文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用以下代码来提取附件的文件名:

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()

它在许多情况下运行良好,但有时作为 image_file_name 我得到像 =?KOI8-R?B?xsHTLTk2Mi5qcGc =?= =?UTF-8?B REkyeTFXMFNMNzAuanBn?=

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?

推荐答案

在以?分隔的三个部分中,使用前两个作为如何处理第三个的说明:

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

第一个位是字符编码(KO18- R和UTF-8),第二位是一个'B'来表示base64编码 - 这个地方的Q会指示引用可打印的,所以你也应该准备你的代码。

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.

这篇关于如何使用python电子邮件获取解码附件文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆