我可以在自动电子邮件中从 Google Form/Google Sheet 发送多个上传的附件吗?

Can I send multiple uploaded attachments from a Google Form/Google Sheet in an automated email?

我正在尝试创建一个链接到 Google Sheet 的 Google 表单,利用一些 Google 应用程序脚本将从调查中获取一些答案,附上上传的文件,并向特定人员发送电子邮件。我已经能够弄清楚我在 Google Sheet 中收集数据并发送电子邮件的部分,但是我获取上传文件并将其作为电子邮件附件的部分是难倒我了。

目前,我发送电子邮件的代码如下所示:

GmailApp.sendEmail(Recipient,subject,'',{htmlBody: htmlText},);

但是看着 the documentation on sendEmail,我似乎想在选项部分添加更多内容,对吗?因此,如果我为此定义一个变量,我需要使用 getFileById,但每次上传的文件 ID 都会不同。此外,我可能需要附加多个文件。

我创建了一个测试Google Form here and I have attached it to a Google Sheet here. You can see the Google App Script here。您可以通过查看代码中指定的 formtesting4@mailinator.com 来检查电子邮件是否成功 sent/received。

我想做的事情可行吗?

您可以参考这个示例脚本:

function emailMe() {

  const ss = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
  const info = ss.getDataRange().getValues();

  info.forEach((entry,a) => {

    // Identify whether notification has been sent
    if (entry[4] === '') {


      // Collect entries
      var Recipient = "your@email.com"
      var subject = 'Roster Scheduler Update';
      Timestamp = entry[0];
      Name = entry[1];
      Roster = entry[2];
      attachment = "TEST";
      
      var attachmentBlobs = [];
      //Get the blob of all attachments available
      if(entry[3]){
        var attachments = entry[3].split(', ');
      
        attachments.forEach(url => {
          var fileId = url.replace('https://drive.google.com/open?id=','');
          Logger.log(fileId);
          var file = DriveApp.getFileById(fileId);
          attachmentBlobs.push(file.getBlob());
        });
      }
 
      let body = '';

      // Generate email
      var html = HtmlService.createTemplateFromFile("email.html");
      var htmlText = html.evaluate().getContent();
      
      // Send email
      GmailApp.sendEmail(Recipient, subject, body, {htmlBody: htmlText, attachments: attachmentBlobs});

      // Email confidence
      const check = 'Sent';
      ss.getRange(a + 1,5).setValue(check);

    }
  });
}

完成的更改:

输出:

示例 1:3 个附件

示例 2:1 个附件