我正在寻找一种简单的解决方案来重命名电子邮件中的附件。 (我在客户电子邮件中存储了大约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
如何改变?