首页 > 解决方案 > 谷歌应用程序脚本发送带有 n 个附件的邮件

问题描述

我似乎找不到任何可以完成我想要做的事情的东西,而且我对 JavaScript 的熟练程度不如我想的那样,所以请多多包涵……正如我在标题中提到的,我在谷歌应用程序脚本中写这个......

如果我有类似的代码:

function getMails() {
  // get the first 5 threads
  var threads = GmailApp.getInboxThreads(0, 5);
  // get all the messages for the threads selected
  var messages = GmailApp.getMessagesForThreads(threads);
  // iterate through the threads
  // ('messages' being the first message of the thread)
  for (var i = 0; i < messages.length; i++) {
    // then iterate through every message of the thread

    var blobs = [];
    for (var j = 0; j < messages[i].length; j++) {
      // create a blob for every message
      var blob = Utilities.newBlob(messages[i][j].getRawContent(),
        'text/plain',
        'message' + j + '.eml');
      // push the blob into the array
      blobs.push(blob);
    }
    // here's where I think I'm having trouble finding a solution
    // and I do not want to zip all the blobbed messages into 1
    MailApp.sendEmail(Session.getActiveUser().getEmail(),
      "thread report: [" + messages[i][0].getSubject() + "]",
      "attached message as text/plain", {
        name: 'Automatic Emailer Script',
        attachments: blobs
      });
  }
}

谁能给我一个指针,告诉我如何为每个线程的可变数量的消息完成这最后一步。

标签: google-apps-script

解决方案


在阅读了评论并重新尝试了代码(并修复了一些我错过的东西,因为我是即时编写的并且没有复制和粘贴我的工作/非工作代码)之后,我让它工作了!感谢大家对您的评论和见解的帮助。问题中的代码已被编辑并按预期工作。


推荐阅读