使用 Google 应用脚本自动转发电子邮件

Automatically Forwarding EMails using Google App Scrpiting

我使用 gmail,我正在努力学习 Javascript。我知道可能还有其他方法,但我想弄清楚这个方法主要是为了了解我的思维过程哪里出了问题。我的目标是学习 JavaScript 和 Google App 脚本。

我在 gmail 中用特定标签标记电子邮件,我最终想将所有这些电子邮件转发给特定的人。人的变化取决于谁在做这项工作。我希望的是:

下面是我所拥有的,我知道我可以清理其中的一些内容(例如,将 columnB 更改为收件人),但我还不能让核心工作。当我尝试 运行 下面的内容时,它将 运行 通过标签中大约一半的消息,然后 return “TypeError: Cannot read 属性 'forward'未定义的 myFunction @ Code.js:21" 这是实际发送消息的行。

        threads[i].getMessages()[i].forward(recipient, {subject: subject}); // Sends e-mail

感谢您的帮助。我已经包含了下面的全部代码,以防它不仅仅是上面的行。谢谢!

function myFunction() {

  var sheetID = 'ID'; //Edit this to your sheet ID.
  let sheet = SpreadsheetApp.openById(sheetID);
  let rowNum = 6; //Need to make this match to labels, but good enough for a POC
  var values = sheet.getActiveSheet().getDataRange().getValues();
  let columnA = values[rowNum][0]; // get column A - Client Name
  let columnB = values[rowNum][1]; // get column B - email
  let columnC = values[rowNum][2]; // get column C - label
  let columnD = values[rowNum][3]; // get column D - archive

// Grab E-Mails to send
  var labels = GmailApp.getUserLabelByName(columnC);
  var archive = GmailApp.getUserLabelByName(columnD);
  var threads = labels.getThreads();
  var length = threads.length;
  let recipient = columnB;
  for (let i = 0; i < length; i++) {
    let subject = columnA + ' - ' + threads[i].getFirstMessageSubject();
    threads[i].getMessages()[i].forward(recipient, {subject: subject}); // Sends e-mail
    Logger.log("Thread ID: " + threads[i].getId() + " Message Sent: " + columnA + " - " + threads[i].getFirstMessageSubject()) // Changes subject to add Client name in front
    archive.addToThread(threads[i]); // Adds the label that is shown in dolumn D of the spreadsheet
    labels.removeFromThread(threads[i]); // removes the inbox label
  }
}

您正在尝试使用相同的索引 i 来获取线程和其中的消息,但线程可能有不同数量的消息。例如,第 10 个线程可能只有 5 条消息,因此如果您尝试访问该线程的第 10 条消息,您将得到 undefined

如果您尝试获取线程的第一条消息(例如,如果它只有一条消息),那么您可以使用 getMessages()[0].

如果要转发所有消息,需要循环遍历getMessages()检索到的消息:

for (let i = 0; i < length; i++) {
  let subject = columnA + ' - ' + threads[i].getFirstMessageSubject();
  let messages =  threads[i].getMessages();
  for (let j = 0; j < messages.length; j++) {
    messages[j].forward(recipient, {subject: subject}); // Sends e-mail
    Logger.log("Thread ID: " + threads[i].getId() + " Message Sent: " + columnA + " - " + threads[i].getFirstMessageSubject()) // Changes subject to add Client name in front
    archive.addToThread(threads[i]); // Adds the label that is shown in dolumn D of the spreadsheet
    labels.removeFromThread(threads[i]); // removes the inbox label
  }
}

视情况而定,您还可以包含 IF 语句以仅转发满足特定条件的邮件。

function myFunction1() {
  const ss = SpreadsheetApp.openById("sheetID");
  const sh = ss.getActiveSheet();
  const vs = sh.getRange(2, 1, sh.getLastRow() - 1, sh.getLastColumn()).getValues();
  vs.forEach((r, i) => {
    let recipient = r[1];
    let labels = GmailApp.getUserLabelByName(r[2]);
    let archive = GmailApp.getUserLabelByName(r[3]);
    let threads = labels.getThreads();
    threads.forEach(t => {
      let subject = `${r[0]} - ${t.getFirstMessageSubject()}`;
      t.getMessages().forEach(m => {
        m.forward(r[1], { subject: subject });
        archive.addToThread(t);
        labels.removeFromThread(t);
      });
    });
  });
}