我有一个使用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') }} ">
有没有任何参考资料或想法说明为什么这些不起作用?
发布于 2019-04-25 03:37:38
您需要在附件中添加“Content-ID”标头值。
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">https://stackoverflow.com/questions/55271348
复制相似问题