使用Python在现有的eml文件中添加pdf附件,可以通过以下步骤完成:
import email
from email.mime.multipart import MIMEMultipart
from email.mime.application import MIMEApplication
with open('existing.eml', 'rb') as f:
msg = email.message_from_bytes(f.read())
new_msg = MIMEMultipart()
new_msg['From'] = msg['From']
new_msg['To'] = msg['To']
new_msg['Subject'] = msg['Subject']
new_msg.attach(msg.get_payload())
with open('attachment.pdf', 'rb') as f:
attachment = MIMEApplication(f.read(), _subtype='pdf')
attachment.add_header('Content-Disposition', 'attachment', filename='attachment.pdf')
new_msg.attach(attachment)
with open('new.eml', 'wb') as f:
f.write(new_msg.as_bytes())
请注意,上述代码中的'existing.eml'和'attachment.pdf'分别代表现有的eml文件和要添加的pdf附件的文件路径。你需要根据实际情况进行替换。
推荐腾讯云相关产品:腾讯云对象存储 COS(Cloud Object Storage)
请注意,上述推荐的产品链接仅供参考,具体的产品选择应根据实际需求和个人偏好进行评估。
领取专属 10元无门槛券
手把手带您无忧上云