I have a simple MailApp to send text in HTML format. The small question I have is: How do I insert inline images in that text? For example, I want to add a Dutch flag for the Dutch text, and a French flag for the French content.
I assumed just using HTML code would do the job. But alas, no such luck. It's just a tiny image I need, no big images below the content. How can I accomplish this?
MailApp.sendEmail(mailaddress, subject, "" ,
{ htmlBody: bodyNL + bodyFR })
From the MailApp docs:
So you need to add your images to the inlineImages advanced parameter, and reference the image key when you compose your HTML message.
The documention for
sendEmail(message)
shows how to add two logos to an email as inline images.Here's an adaptation from Google's example that will prepend your text blocks with their locale flags. It uses images from Wikimedia, which are freely licensed under Creative Commons.
The keys to how this works are:
<img>
tags in the email body usesrc='cid:[name]'
to reference attached images. These are Content-ID Universal Resource Locators, which reference parts of multipart email messages.style
on the<img>
tags to tell the mail client how to display them. For example, we've scaled the attached images. (Note: it would be less wasteful to use custom images that are the exact size needed.)When the email is encoded for sending, each attached image will be in its own part, labelled with the
cid
you've provided in theinlineImages
object. This object is part of the advanced parameters object for thesendMail()
method.Here, we have two
cid
s,nlFlag
andfrFlag
, each associated with a fileBlob
.File blobs can be obtained in many ways; here we've used
UrlFetchApp.fetch()
to get the binary images from WikiMedia, then converted them to blobs and set the name of the blob for attaching.Code: