首页 > 解决方案 > 将 Google Drive 中的图片添加到 html 电子邮件正文

问题描述

是否可以将 google 磁盘中的 png / gif 文件添加到 google 脚本生成的电子邮件 htmlbody 中?我有如下代码,该文档是公开共享的,启用了 Drive API 和 Gmail API。这是从磁盘共享文件的问题还是我的代码中的错误,请帮助

function sendaEmail(){
  var templ = HtmlService
  .createTemplateFromFile('template');
  
  var message = templ.evaluate().getContent();
  
  MailApp.sendEmail({
    to: 'email@gmail.com',
    subject: "subject",
    htmlBody: message
  });
}
<!DOCTYPE XHTML 1.0 Transitional//EN">
<html>
 <head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
      <title>Title</title>
    <meta name="viewport" content="width=device-width, initial-scale=1.0"/>
  </head>
  <body style="margin: 0; padding: 0;">
    <table border="0" cellpadding="0" cellspacing="0" width="100%">
      <tr>
        <td>
          <table align="center" border="0" cellpadding="0" cellspacing="0" width="600" style="border-collapse: collapse;">
            <tr>
              <td align="center" bgcolor="#70bbd9" style="padding: 40px 0 30px 0;">
                <img src= <?= DriveApp.getFileById("1tKxUgeX_B27vnpNNpKy1jF_0zOxrBhmL").getAs("image/gif");?> alt="Creating Email" width="300" height="230" style="display: block;" />
              </td>
            </tr>
          </table>
        </td>
      </tr>
    </table>
  </body>
</html>

标签: javascripthtmlgoogle-apps-script

解决方案


首先,您应该调用 getUrl(),而不是 getAs(),getAs() 方法返回一个二进制对象而不是 Url,因为您应该在 HTML 标记的 src 属性上添加一个 URL。

但是,无论如何,如果您尝试它,它不会专门用于 Google Drive 链接,因为 DriveApp Urls 不是规范的,并且不打算像这样附加到 gmail 服务上。

那么,我们如何在来自驱动器的电子邮件中附加内联图像?

最简单的方法如下:

function sendaEmail(){
  var templ = HtmlService
  .createTemplateFromFile('template');

  var message = templ.evaluate().getContent();

  var imageId = DriveApp.getFileById("YOURIMAGEID");
  var imageBlob = imageId.getBlob();  

  MailApp.sendEmail('your@email.com', "Subject",  "Hi", {htmlBody: message, inlineImages: {image: imageBlob}});

}

所以,你在这里做的是直接附加图像对象,JS会处理这个,如何?我们必须修改 HTML 模板:

<!DOCTYPE html>
<html>
  <head>
    <base target="_top">
  </head>
  <body>
     <table border="0" cellpadding="0" cellspacing="0" width="100%">
      <tr>
        <td>
          <table align="center" border="0" cellpadding="0" cellspacing="0" width="600" style="border-collapse: collapse;">
            <tr>
              <td align="center" bgcolor="#70bbd9" style="padding: 40px 0 30px 0;">
                <img src= "cid:image" alt="Creating Email" width="300" height="230" style="display: block;" />
              </td>
            </tr>
          </table>
        </td>
      </tr>
    </table>
  </body>
</html>

这会做,还有其他方法,但这很简单。

在这里,您将找到有关在电子邮件中嵌入图像的几种方法的一些信息: https ://blog.mailtrap.io/embedding-images-in-html-email-have-the-rules-changed/

如果您需要添加更多图像,请执行以下操作:

  var imageId2 = DriveApp.getFileById("15FhCLXu6bQE70iNRCXVAve0TyyoeoUTA");
  var imageBlob2 = imageId2.getBlob(); 
  MailApp.sendEmail('your@email.com', "Subject",  "Hi", {htmlBody: message, inlineImages: {image: imageBlob, image2: imageBlob2}});

现在在 html 上添加另一个标签

    <img src= "cid:image2" alt="Creating Email" style="display: block;" />


推荐阅读