如何使用 MailApp 在电子邮件中包含内联图像

How to include inline images in email using MailApp

我有一个简单的 MailApp 可以发送 HTML 格式的文本。我有一个小问题:如何在该文本中插入内联图像?例如,我想为荷兰语文本添加一个荷兰国旗,为法语内容添加一个法国国旗。

我假设只使用 HTML 代码就可以完成这项工作。但是,唉,没有这样的运气。这只是我需要的一个小图像,内容下方没有大图像。我怎样才能做到这一点?

MailApp.sendEmail(mailaddress, subject, "" ,
              { htmlBody: bodyNL + bodyFR })

来自 MailApp 文档:

htmlBody String if set, devices capable of rendering HTML will use it instead of the required body argument; you can add an optional inlineImages field in HTML body if you have inlined images for your email

inlineImages Object a JavaScript object containing a mapping from image key (String) to image data (BlobSource); this assumes that the htmlBody parameter is used and contains references to these images in the format

因此您需要将图像添加到 inlineImages 高级参数,并在撰写 HTML 消息时引用图像键。

sendEmail(message) 的文档显示了如何将两个徽标作为内联图像添加到电子邮件中。

这是对 Google 示例的改编,它将在您的文本块前面加上它们的区域设置标志。它使用来自 Wikimedia 的图像,这些图像根据 Creative Commons 免费许可。

其工作原理的关键是:

    电子邮件正文中的
  • HTML <img> 标签使用 src='cid:[name]' 引用附加图像。这些是 Content-ID 通用资源定位符,它们引用多部分电子邮件的部分内容。
  • 您可以在 <img> 标签上使用内联 style 来告诉邮件客户端如何显示它们。例如,我们已经缩放了附加图像。 (注意:使用完全符合所需尺寸的自定义图像会减少浪费。)
  • 当电子邮件被编码发送时,每张附加图像将在其自己的 部分 ,并标有您提供的 cidinlineImages 对象中。此对象是 sendMail() 方法的高级参数对象的一部分。

    在这里,我们有两个 cidnlFlagfrFlag,每个都与一个 file Blob.

    相关联
    inlineImages:
    {
      nlFlag: nlFlagBlob,
      frFlag: frFlagBlob
    }
    
  • 文件blob可以通过多种方式获得;这里我们使用 UrlFetchApp.fetch() 从 WikiMedia 获取二进制图像,然后将它们转换为 blob 并设置要附加的 blob 的名称。

代码:

/**
 * Example of sending an HTML email message with inline images.
 * From: 
 */
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
                      }
                    });

}