我正在寻找一种简单的解决方案来重命名电子邮件中的附件。(我已在客户电子邮件中存储了约 11k CSV 文件并希望重命名这些文件)
import getpass, imaplib, email, smtplib
...
for item in email_body:
m = email.message_from_string(item)
mail_date = m['date']
mail_subject = m['subject']
if m.get_content_maintype() != 'multipart':
continue
for part in m.walk():
if part.get_content_maintype() == 'multipart':
continue
if part.get('Content-Disposition') is None:
continue
# the filename
filename=part.get_filename()
# my attachment
payload = part.get_payload(decode=True)
我在这里卡住了。我认为有一种方法称为 part.set_filename 或类似的方法,但我找不到它。
以下是邮件的摘要:
----next32sdfsdfsdg827463sbc
Content-Type: application/octet-stream; name=HU014_W1345.CSV
Content-Transfer-Encoding: base64
Content-Disposition: attachment; filename=HU014_W1345.CSV
我测试了这样的东西:
print part['Content-Disposition']
包含文件名:
attachment; filename=HU002_W1342.CSV
如何改变它?