Tuesday, January 31, 2017

Can you and how do you embed images in an email when using the Gmail API?

Leave a Comment

When creating a message and using it to create a draft or email using the Gmail API, can you have an image embedded in the body? I'm looking to have the image data actually embedded similar to how copying and pasting an image (the actual data, not the link) into a Gmail email will place the image right in the content.

Can it be done like this or do I need to upload the image to some other location and use HTML to embed the image in the email? Any pointers on how to do it?

1 Answers

Answers 1

The short answer is that you would do this the same way you would for any email service.

The long answer is that you need to create a multipart/related message, where one part is the HTML content of the email and the other part is the image. The image part contains a Content-ID header that specifies an ID for the image, and the HTML image tag references that ID in the src attribute using the format cid:ID_HERE.

An example of how to construct such an email in Python is here: http://stackoverflow.com/a/1633493

P.S. - A great way to see how emails are constructed is to look at the raw message. You can look at the raw message for a given email in Gmail by clicking the drop down arrow next to the message and selecting "Show original".

If You Enjoyed This, Take 5 Seconds To Share It

0 comments:

Post a Comment