Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 387
Brett Lesnau Avatar asked Jun 27 '14 21:06

Brett Lesnau


People also ask

Can you embed an image in Gmail?

Log in to your Gmail account, compose a new email or reply, and click where you want the image to appear. Move your mouse cursor over the "+" or paper clip icon to reveal additional options. Click the "Insert Photos" camera icon. Click "Browse" and double-click an image from your computer.

How do you embed an image into an email?

Position your cursor where you want the image in your message. Select Insert > Pictures. Browse your computer or online file locations for the picture you want to insert. Select the picture, then select Insert.

How do you paste an image in the body of an email in Gmail?

Here's how you do it. Find a cool image you want to email. Right click and select “Copy image”. Paste it into your message.

Does Gmail block embedded images?

Google's Gmail website automatically blocks many email images until it's sure the messages are coming from a trusted source. You can't instruct Gmail to always show images from all senders, but you can allow images from certain senders by clicking "Always Display Images From …" at the top of their messages.


1 Answers

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: https://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".

like image 112
Eric Koleda Avatar answered Oct 13 '22 19:10

Eric Koleda