How to embed base64 images into SendGrid email via python?

Viewed 1720

According to this article by Sendgrid, inline embedding (Base64 Encoding) is supported.

However the below code does not work, because it does not show the actual image, which should look like this.

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)

The email I received looks like the following: enter image description here

1 Answers

Your code might be working, since the alt attribute is being displayed.

However Gmail (and most other email clients cannot display embedded SVG images in an email.


Not sure how much creative freedom you have, but you could use special characters display a red • :

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

or

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