我有一个 Django 项目,我正在开发一个电子邮件客户端。我决定使用 python 的IMAPClient而不是标准库imaplib
来访问消息。目前,我没有使用python 的 email 包来编码/解码从IMAPClient收到的响应,我觉得我手动实现了应该由email处理的事情。
下载附件示例代码:
def download_attachment(server, msgid, index, encoding):
# index and encoding is known from previous analysis of bodystructure
file_content = f_fetch(server, msgid, index)
# the below code should be handled by email's message_from_bytes
# and subsequent get_payload(decode = True) function
if encoding == 'base64':
file_content = base64.b64decode(file_content)
elif ...
...
endif
#writing file_content to a folder
return
def f_fetch(server, msgid, index):
if not index:
index = '1'
response = server.fetch(msgid, 'BODY[' + index + ']')
key = ('BODY[' + index + ']').encode('utf-8')
if type(msgid) is str:
msgid = int(msgid)
return response[msgid][key]
所以问题是,我应该如何重写这段代码来使用email。具体来说,我应该如何处理来自 IMAPClient 的响应以将其传递给电子邮件的 message_from_bytes() 函数?