根据指定条件删除邮件



我正在研究一个基于时间删除电子邮件的google apps脚本。我设置了一个每日日程,但过了一段时间它们就会填满我的收件箱。

每日日程邮件

我正在考虑每天自动删除这些电子邮件,因为我每天早上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(/[^sw"!,?']/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");
}
}

相关内容

  • 没有找到相关文章

最新更新