在python中发送HTML图像电子邮件

问题描述

我正在尝试发送带有内嵌图像的 HTML 电子邮件,但图像没有出现,对于某些系统,它工作正常并且图像正在显示,但对于某些系统,它没有出现,请检查附加的图像以供参考。

Sample image error occuring

而且我也尝试使用数据 uri 绕过图像

<td>
    <img style="display:block;"   src ="{{ image_tag }}" alt="check" 
    width="100%"></p>
</td>
data_uri= base64.b64encode(open('C://Users//Desktop//work.png','rb').read()).decode('utf-8')
image_tag = "data:image/png;base64,"+ data_uri
with open('C://Users//Desktop//final.html',encoding='utf-8-sig') as myfile:
    outerdata=myfile.read()
    template = Template(outerdata)
    finalpage=template.render(image_tag = image_tag)

解决方法

正如 mousetail 所说,许多电子邮件客户端不支持 base64 编码 https://www.caniemail.com/features/image-base64/

您几乎正确:

<td> <img src="C:\Users\Desktop\work.png" alt="Capgemini" style="display:block; margin-left:auto; margin-right:auto;"> </td>

但首先将图像上传到互联网,在一个公开可用的地方(即某个网站)。那么 src 应该是 src="https://www.somewhere.com/image.png"

这是未说明的,但是,如果您希望图像居中,您应该在块元素上执行此操作,而不是尝试将 <img> 更改为块。原因是 Outlook 桌面不支持将内联元素更改为阻止,反之亦然。