【问题标题】:Can you give examples for attachments & inlineImages in Google Apps Script?您能否提供 Google Apps 脚本中的附件和内联图像示例?
【发布时间】:2021-09-18 08:08:57
【问题描述】:

Class GmailMessage 主要接受简单的高级参数类型,如字符串和布尔值,除了:

Name Type Description
attachments BlobSource[] an array of files to send with the 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

你能举出这两个的例子吗?

例如,用var obj 中的示例值替换点:

// 1x1 image that can be used:
// data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAIAAACQd1PeAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAADsMAAA7DAcdvqGQAAAAMSURBVBhXY/j//z8ABf4C/qc1gYQAAAAASUVORK5CYII=

var obj = {attachments: [...], inlineImages: ...];
message.forward('someone@somewhere.com', obj);

【问题讨论】:

    标签: google-apps-script blob gmail-api


    【解决方案1】:

    我使用一些类似的功能从我的 Google 照片库中插入图片:

    function sendEmails103(obj) {
      const ss = SpreadsheetApp.getActive();
      const sh = ss.getSheetByName('libImages');
      const [hA, ...dt] = sh.getDataRange().getValues();
      let idx = {};
      hA.forEach((h, i) => idx[h] = i);
      let imgObj = {};
      vs = dt.filter(r => r[idx['filename']] == obj.row[obj.index['htmlFile']])
      vs.forEach((r, i) => {
        let params = { muteHttpExceptions: true, headers: { "Authorization": "Bearer " + ScriptApp.getOAuthToken() } };
        let aurl = "https://photoslibrary.googleapis.com/v1/mediaItems/" + r[idx['mediaItemId']]
        let resp = JSON.parse(UrlFetchApp.fetch(aurl, params).getContentText());
        let burl = `${resp.baseUrl}=w${r[idx['maxwidth']]}-h${r[idx['maxheight']]}`
        imgObj[r[idx['Key']]] = UrlFetchApp.fetch(burl).getBlob();
      });
      let htmlTemplate = HtmlService.createTemplateFromFile(obj.row[obj.index['htmlFile']]);
      let html = htmlTemplate.evaluate().getContent();
      if (html) {
        if (obj.row[obj.index['operation']] == 'Create Draft') {
          GmailApp.createDraft(obj.row[obj.index['Recipients']], obj.row[obj.index['Subject']], '', { htmlBody: html, inlineImages: imgObj, replyTo: obj.row[obj.index['replyTo']] });
        } else {
          GmailApp.sendEmail(obj.row[obj.index['Recipients']],obj.row[obj.index['Subject']],'',{htmlBody:html,inlineImages:imgObj,replyTo: obj.row[obj.index['replyTo']]});
        }
      }
    }
    

    我将 mediaItemIds 和密钥保存在这样的电子表格中:

    Key Description mediaItemId maxwidth maxheight filename
    img0 Redacted Redacted 384 384 file103
    img1 Redacted Redacted 384 384 file103
    img2 Redacted Redacted 384 384 file103
    img3 Redacted Redacted 384 384 file103
    img4 Redacted Redacted 384 384 file103
    img5 Redacted Redacted 384 384 file103
    img6 Redacted Redacted 384 384 file103
    img7 Redacted Redacted 384 384 file103

    这是一个使用我的云端硬盘中的图像的简单示例:

    function sendEmails101(obj) {
      let imgObj = {};
      let fldr = DriveApp.getFolderById("folderid");
      let files = fldr.getFilesByType(MimeType.JPEG);
      let n = 0;
      let names = ['die1.jpg', 'die2.jpg'];
      let filename = [];
      while (files.hasNext()) {
        let f = files.next();
        let index = names.indexOf(f.getName());
        if (~index) {
          imgObj[`img${n++}`] = f.getBlob();
          filename.push(names[index]);
        }
      }
      let htmlTemplate = HtmlService.createTemplateFromFile(obj.row[obj.index['htmlFile']]);
      htmlTemplate.values = obj.row.slice(obj.index['Data0']);
      htmlTemplate.filename = filename;
      let html = htmlTemplate.evaluate().getContent();
      if (html) {
        if (obj.row[obj.index[operation]] == 'Create Draft') {
          GmailApp.createDraft(obj.row[obj.index['Recipients']], obj.row[obj.index['Subject']], '', { htmlBody: html, inlineImages: imgObj, replyTo: obj.row[obj.index['replyTo']] });
        } else {
    
          GmailApp.sendEmail(obj.row[obj.index['Recipients']], obj.row[obj.index['Subject']], '', { htmlBody: html, inlineImages: imgObj, replyTo: obj.row[obj.index['replyTo']] });
        }
      }
    }
    

    【讨论】:

    • 这是inlineImages 的一个很好的例子,你也有一个attachments 的例子吗?我已经对其进行了测试,因此,如果您愿意,只需编辑您的答案并添加{attachments: UrlFetchApp.fetch(burl).getBlob()} - 它可以工作。那我就接受了。
    • 能否请您添加答案以便我接受?
    猜你喜欢
    • 1970-01-01
    • 2015-01-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-10-06
    相关资源
    最近更新 更多