我有一个使用 Flask
的应用程序和 Flask-Mail
.我正在尝试发送一封电子邮件,该电子邮件使用 html 文件作为带有内嵌图像的模板。一切正常,除了我看不到图像。我的代码是:
msg = Message(subject, sender=sender, recipients=recipients)
msg.body = text_body
msg.html = html_body
msg.attachments = [
Attachment(
'header.gif',
'image/gif',
open(join(mail_blueprint.static_folder, 'header.gif'), 'rb').read(),
'inline'
)
]
mail.send(msg)
对于 html 文件,我尝试通过几种方式引用它,例如:
<img src="cid:footer.gif">
和<img src="{{ url_for('mail.static', filename='header.gif') }} ">
任何引用或想法为什么这些不起作用?
最佳答案
您需要在附件中添加“Content-ID” header 值。
msg = Message(subject, sender=sender, recipients=recipients)
msg.body = text_body
msg.html = html_body
msg.attach('header.gif','image/gif',open(join(mail_blueprint.static_folder, 'header.gif'), 'rb').read(), 'inline', headers=[['Content-ID','<Myimage>'],])
mail.send(msg)
然后在您的模板中,您将通过 Content-ID 引用附件。
<img src="cid:Myimage">
关于python-3.x - 发送带有内嵌图像的电子邮件 Flask-Mail?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55271348/