我需要从电子邮件中提取一个味精类型的附件,并将味精附件保存到 python 中的某个位置。
我编写的脚本适用于除 Outlook 项目之外的几乎所有类型的文件
def parse_attachment(message_part):
content_disposition = message_part.get("Content-Disposition", None)
if content_disposition:
dispositions = content_disposition.strip().split(";")
if bool(content_disposition and (dispositions[0].lower() == "attachment" or dispositions[0].lower() == "inline")):
file_data = message_part.get_payload(decode=True)
debug(message_part)
attachment = {}
attachment['data'] = file_data
attachment['content_type'] = message_part.get_content_type()
attachment['size'] = len(file_data)
for param in dispositions[1:]:
name,value = param.split("=")
name = name.lower().strip()
value = value.strip().strip("\"")
if name == "filename":
attachment['name'] = value
elif name == "creation-date":
attachment['creation-date'] = value
elif name == "modification-date":
attachment['modification-date'] = value
elif name == "size":
attachment['size'] = value
return attachment
return None