如何将内嵌图像放入来自Google工作表的自动邮件中

时间:2019-02-15 09:41:53

标签: google-apps-script google-sheets

我想发送带有嵌入式图像的邮件,而不是通过Google表格发送附件。请帮助

脚本如下:

function emailSummary() {
    var ss = SpreadsheetApp.getActiveSpreadsheet()
    var sh = ss.getSheetByName("Hitesh")
    var file = DriveApp.getFileById('1T8QA_WsXQkZZGwmBSN13iPV7rM8xGG_GtYy6T1eug-c') 
    var gmail = 'hitesh.gtg@Gmail.com';
    var html =  '<png>'

    MailApp.sendEmail("hitesh.gtg@gmail.com", "sunject", "Dear Sir,\n\n Forwarding herewith Monthly Summaryw \n\n Regards \n Hitesh ", {
    //email address, subject, message

     name: 'Hitesh', //file name
     attachments: [file.getAs(MimeType.PDF)] //file to attach
    });
}

2 个答案:

答案 0 :(得分:0)

您可以pass HTML进入.sendMail()功能。官方文档的此链接包括嵌入式图像的示例!

// This code fetches the Google and YouTube logos, inlines them in an email
// and sends the email
function inlineImage() {
  var googleLogoUrl = "http://www.google.com/intl/en_com/images/srpr/logo3w.png";
  var googleLogoBlob = UrlFetchApp
                         .fetch(googleLogoUrl)
                         .getBlob()
                         .setName("googleLogoBlob");

  //You can also get these images from your drive to attach them.

  var imgBlob = DriveApp.getFileById("<DRIVE_FILE_ID_OF_PICTURE>")
                     .getBlob()
                     .setName("imgBlob");


  MailApp.sendEmail({
    to: "recipient@example.com",
    subject: "Logos",
    htmlBody: "inline Google Logo<img src='cid:googleLogo'> images! <br>" +
              "img from drive: <img src='cid:driveImg'>"
    inlineImages:
      {
        googleLogo: googleLogoBlob,
        driveImg: imgBlob
      }
  });
}

答案 1 :(得分:0)

通过Google云端硬盘通过电子邮件发送图像

您可以在htmlBody中使用GmailApp.sendMail()参数。但是,如果要避免将图像存储在可公开访问的URL中。您可以执行以下操作。

这是我的JavaScript的一部分:

 function sendImgMsg() {
    var fileId=$('#mediaSel').val();//This is the fileId where the image is store.  In my image converter script I keep all of this images in the same folder.
    google.script.run
    .withSuccessHandler(function(fObj){
      var msg=$('#emsg').val();//This is the contents of a textarea
      var hl='<p>' + msg + '</p><br /><strong>File Name:</strong> '+ fObj.name + '<img src="'+ fObj.uri +'" title="' + fObj.filetype + '" />';
      $('#email').css('display','none');
      google.script.run.sendImageMessage(hl);//This is the code that sends the email
    })
    .getSelectedFile(fileId);
  }

这是我的html的一部分:

<div id="email">
   <textarea id="emsg" cols="40" rows="4"></textarea>
   <br /><input type="button" value="Send" onClick="sendImgMsg()" />
</div>

这是我代码的一部分。gs:

function getSelectedFile(fileId){
  var file=DriveApp.getFileById(fileId);
  var dataURI=file.getBlob().getDataAsString();
  var s=dataURI.split(',')[0];
  var mediaType=s.slice(s.indexOf(':')+1,s.indexOf('/'));
  var fileType=s.slice(s.indexOf('/')+1,s.indexOf(';'));
  var fObj={name:file.getName(),uri:dataURI ,type:mediaType,filetype:fileType};
  return fObj;
}

function sendImageMessage(hl) {
  GmailApp.sendEmail('recipient', 'ImageInAnEmail', null ,{htmlBody: hl});
}

这是将外部图像转换为imageURI的代码:

function convImageUrl(url){
  var blob=UrlFetchApp.fetch(url).getBlob();
  var b64Url='data:' + blob.getContentType() + ';base64,' + Utilities.base64Encode(blob.getBytes());
  return b64Url;
}

以上是脚本的一部分,该脚本用于将图像转换为imageURI,以便可以在Google云端硬盘上存储和访问它们。希望有帮助。