前段时间,我在Python上编写了一个处理电子邮件消息的程序,总有一件事就是知道电子邮件是否是"多部分".
经过一些研究,我知道它与包含HTML或附件等的电子邮件有关......但我并不是真的理解它.
1.当我必须从原始电子邮件中保存附件时
我刚刚在互联网上发现了这一点(可能在这里 - 很抱歉没有记下编写它的人,但我似乎无法再找到他了:/)并将其粘贴在我的代码中
def downloadAttachments(emailMsg, pathToSaveFile):
"""
Save Attachments to pathToSaveFile (Example: pathToSaveFile = "C:\\Program Files\\")
"""
att_path_list = []
for part in emailMsg.walk():
# multipart are just containers, so we skip them
if part.get_content_maintype() == 'multipart':
continue
# is this part an attachment ?
if part.get('Content-Disposition') is None:
continue
filename = part.get_filename()
att_path = os.path.join(pathToSaveFile, filename)
#Check if its already there
if not os.path.isfile(att_path) :
# finally write the stuff
fp = open(att_path, 'wb') …Run Code Online (Sandbox Code Playgroud)