我使用python-mime库在邮件中发送多个文件附件。我试图使用add_header函数设置一些值"内容描述"字段,但无法设置。
https://docs.python.org/3/library/email.compat32-message.html#email.message.Message.add_header
代码段
msg.add_header('Content-Type','text/html')
msg.add_header('Content-Disposition', 'attachment', filename="intrusion.html")
msg.add_header`('Content-`Description','This is an Mail Attachment')
请告知如何添加页眉。
Content Type和Content Disposition标头由EmailMessage.add_attachment自动设置。其他标头,如Content Description,可以作为冒号分隔的列表传递;标题名称:内容;使用headers
关键字参数的字符串。请参阅由add_attachment
调用的ContentManager.set_content的文档。
此示例代码:
from email.message import EmailMessage
# Create the container email message.
msg['Subject'] = 'This message has an attachment'
msg['From'] = 'me@example.com'
msg['To'] = 'you@example.com'
msg.add_attachment(
'<p>hello world</p>',
subtype='html',
filename='instrusion.html',
headers=['Content-Description:This is an attachment'],
)
print(msg.as_string())
产生此输出:
Subject: This message has an attachment
From: me@example.com
To: you@example.com
Content-Type: multipart/mixed; boundary="===============1754799949587534235=="
--===============1754799949587534235==
Content-Type: text/html; charset="utf-8"
Content-Description: This is an attachment
Content-Transfer-Encoding: 7bit
Content-Disposition: attachment; filename="instrusion.html"
MIME-Version: 1.0
<p>hello world</p>
--===============1754799949587534235==--