根据特定条件删除 gmail

Delete gmails based on specific criteria

我正在研究 google 应用脚本来根据时间删除电子邮件。 我已经设置了每日议程,但一段时间后它们就会填满我的收件箱。

daily agenda e-mail

我正在考虑每隔一天自动删除这些电子邮件,因为我每天早上 5 点后都会收到更新,也许设置一个脚本在第二天早上 5 点之前删除它们应该可行,但我还没有找到解决方案为此。

删除所有不在我的白名单上的电子邮件

function deleteAllEmailsNotWhiteListed() {
  const acct = getGlobal('abrevEmail');
  var idA = [];
  GmailApp.getTrashThreads().forEach(t => { Gmail.Users.Threads.remove("me", t.getId()) });
  for (let k = 0; k < 2; k++) {
    switch (k) {
      case 0:
        var threads = GmailApp.getInboxThreads();
        break;
      case 1:
        var threads = GmailApp.getSpamThreads();
        break;
    }
    if (threads) {
      for (let i = 0; i < threads.length; i++) {
        const messages = threads[i].getMessages();
        if (messages) {
          for (let j = 0; j < messages.length; j++) {
            let msg = messages[j];
            let id = msg.getId();
            let sub = msg.getSubject().replace(/[^\s\w"!,?']/g, '');
            let from = msg.getFrom();
            let to = msg.getTo();
            let body = msg.getBody();
            let m = { id: id, from: from, to: to, subject: sub, account: acct }
            if (!isWhiteListed(m)) {
              idA.push(id);
            }
          }
        }
      }
    }
  }
  if (idA.length > 0) {
    var request = { "ids": idA };
    Gmail.Users.Messages.batchDelete(request, "me");
  }
}

function isWhiteListed(msg) {
  const ss = SpreadsheetApp.openById(getGlobal('ssid'));
  const sh = ss.getSheetByName(getGlobal('wlid'));
  const wlog = ss.getSheetByName(getGlobal('wlog'));
  const sr = 2;
  const rg = sh.getRange(sr, 1, sh.getLastRow() - sr + 1, 1);
  const wl = rg.getDisplayValues().flat().map(e => e.toLowerCase());
  if (wl.length == 0) throw ('Error: WhiteList has no content');
  let obj = decomposeEmailsWithTitle({ raw: msg.from, email: '', title: '' });
  let status = wl.indexOf(obj.email.toLowerCase()) > -1;
  wlog.appendRow([Utilities.formatDate(new Date(), Session.getScriptTimeZone(), "E MMM dd, yyyy HH:mm:ss"), obj.email, msg.to, msg.subject, status, msg.account]);
  return status;
}

这是一个只关注删除早于当前日期的电子邮件的版本

function deleteAllEmailsNotWhiteListed() {
  const dt = new Date();
  const ydtv = new Date(dt.getFullYear(),dt.getMonth(), dt.getDate()-1).valueOf();
  const acct = getGlobal('abrevEmail');
  var idA = [];
  GmailApp.getTrashThreads().forEach(t => { Gmail.Users.Threads.remove("me", t.getId()) });
  for (let k = 0; k < 2; k++) {
    switch (k) {
      case 0:
        var threads = GmailApp.getInboxThreads();
        break;
      case 1:
        var threads = GmailApp.getSpamThreads();
        break;
    }
    if (threads) {
      for (let i = 0; i < threads.length; i++) {
        const messages = threads[i].getMessages();
        if (messages) {
          for (let j = 0; j < messages.length; j++) {
            let msg = messages[j];
            let id = msg.getId();
            let d = msg.getDate();
            let dtv = new Date(d.getFullYear(),d.getMonth(),d.getDate()).valueOf()
            if (dtv <= ydtv) {//modifed
              idA.push(id);
            }
          }
        }
      }
    }
  }
  if (idA.length > 0) {
    var request = { "ids": idA };
    Gmail.Users.Messages.batchDelete(request, "me");
  }
}