如何通过Python将base64图片嵌入SendGrid电子邮件?

3
根据Sendgrid的这篇文章,支持内联嵌入(Base64编码)。
然而,下面的代码不起作用,因为它没有显示实际的图像,应该看起来像这样
import sendgrid

html = """
<div>
    <p>Taken from wikpedia</p>
    <img src="data:image/png;base64, iVBORw0KGgoAAAANSUhEUgAAAAUA
AAAFCAYAAACNbyblAAAAHElEQVQI12P4//8/w38GIAXDIBKE0DHxgljNBAAO
    9TXL0Y4OHwAAAABJRU5ErkJggg==" alt="Red dot" />
</div>
"""
sg = sendgrid.SendGridClient('api_key')
message = sendgrid.Mail()
message.set_html(html)
message.add_to('test@gmail.com')
message.set_from('test2@gmail.com')
message.set_subject('test subject')
print sg.send(message)

我收到的电子邮件如下图所示: enter image description here

你用什么邮件客户端查看邮件?Gmail? - Justin Steele
@JustinSteele - 是的,我在使用Gmail。 - Chris
1
我想知道Gmail是否会阻止/不支持内嵌图片。 - Justin Steele
1个回答

0

你的代码可能是有效的,因为alt属性正在显示。

然而Gmail(以及大多数其他电子邮件客户端)无法在电子邮件中显示嵌入的SVG图像


不确定您有多少创意自由,但您可以使用特殊字符显示红色的• :

<p>Taken from wikpedia <span style="color: red;">•</span></p>

或者

<p>Taken from wikpedia <span style="color: red;">&bull;</span></p>

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接