How to include embedded images in email using MailApp

I have a simple MailApp to send HTML text. The small question I have is: How do I embed inline images in this text? For example, I want to add the Dutch flag for Dutch text and the French flag for French content.

I assumed that using HTML would do the job. But alas, there is no such luck. This is just a tiny image that I need, no large images below the content. How can i do this?

MailApp.sendEmail(mailaddress, subject, "" ,
              { htmlBody: bodyNL + bodyFR })
+4
source share
2 answers

sendEmail(message) , .

Google, . , Creative Commons.

screenshot

, :

  • HTML <img> src='cid:[name]' . Content-ID, .
  • inline style <img>, , . , . (: , .)
  • , , cid, inlineImages. sendMail().

    cid s, nlFlag frFlag, Blob.

    inlineImages:
    {
      nlFlag: nlFlagBlob,
      frFlag: frFlagBlob
    }
    
  • ; UrlFetchApp.fetch() WikiMedia, blob blob .

:

/**
 * Example of sending an HTML email message with inline images.
 * From: http://stackoverflow.com/a/37688529/1677912
 */
function sendInlineImages() {
  var mailaddress = Session.getActiveUser().getEmail();
  var subject = "test inline images";
  var bodyNL = "This is <B>DUTCH</B> text.";
  var bodyFR = "This is <B>FRENCH</B> text.";

  // Image URLs, under CC license
  var nlFlagUrl = "https://upload.wikimedia.org/wikipedia/commons/thumb/b/b2/Flag_of_the_Netherlands.png/320px-Flag_of_the_Netherlands.png";
  var frFlagUrl = "https://upload.wikimedia.org/wikipedia/en/thumb/c/c3/Flag_of_France.svg/320px-Flag_of_France.svg.png";

  // Fetch images as blobs, set names for attachments
  var nlFlagBlob = UrlFetchApp
                            .fetch(nlFlagUrl)
                            .getBlob()
                            .setName("nlFlagBlob");
  var frFlagBlob = UrlFetchApp
                            .fetch(frFlagUrl)
                            .getBlob()
                            .setName("frFlagBlob");

  // Prepend embeded image tags for email
  bodyNL = "<img src='cid:nlFlag' style='width:24px; height:16px;'/>" + bodyNL;
  bodyFR = "<img src='cid:frFlag' style='width:24px; height:16px;'/>" + bodyFR;

  // Send message with inlineImages object, matching embedded tags.
  MailApp.sendEmail(mailaddress, subject, "",
                    { htmlBody: bodyNL + "<BR/><BR/>" + bodyFR,
                      inlineImages:
                      {
                        nlFlag: nlFlagBlob,
                        frFlag: frFlagBlob
                      }
                    });

}
+5

MailApp:

htmlBody , , , HTML, ; inlineImages HTML,

inlineImages JavaScript-, (String) (BlobSource); , htmlBody

, inlineImages HTML.

0

All Articles