【发布时间】:2017-08-23 05:44:56
【问题描述】:
我有一个 Django 项目,我正在开发一个电子邮件客户端。我决定使用 python 的 IMAPClient 而不是标准库的 imaplib 来访问消息。目前,我没有使用python's email package 来编码/解码从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() 函数?
【问题讨论】:
-
如果你想使用email包,你可能应该直接使用imaplib,因为IMAPClient已经解析了邮件。
-
@Serge,是不是说只要我用IMAPClient,就应该忘记
email包? -
不确定(它是评论而不是答案的原因......)。我不使用 IMAPClient,但我认为它的主要兴趣之一是解析消息。如果您不需要,我只是不确定您如何使用 IMAPClient,以及为什么它比 imaplib 更好。很抱歉不能为您提供更多帮助...
-
好的。谢谢你)
标签: python email imap imapclient