【发布时间】:2021-12-19 08:41:09
【问题描述】:
我可以使用 mime-content 下载 eml 文件。我需要编辑这个 eml 文件并删除附件。我可以查找附件名称。如果我理解正确,首先是电子邮件标题、正文,然后是附件。我需要有关如何从电子邮件正文中删除附件的建议。
import email
from email import policy
from email.parser import BytesParser
with open('messag.eml', 'rb') as fp: # select a specific email file
msg = BytesParser(policy=policy.default).parse(fp)
text = msg.get_body(preferencelist=('plain')).get_content()
print(text) # print the email content
for attachment in attachments:
fnam=attachment.get_filename()
print(fnam) #print attachment name
【问题讨论】:
-
stackoverflow.com/questions/1626403/… 对于 Python 2 来说基本上是同一个问题,但是由于
emailAPI 从那时起发生了很大变化,我在这里发布了一个新答案,并在旧问题上留下了一个指针. -
关于理解邮件结构,大概参考stackoverflow.com/questions/48562935/…
标签: python email mime-types eml