Last active
January 23, 2020 16:08
-
-
Save jpotisch/302f4321c1976004952428e17610d3e0 to your computer and use it in GitHub Desktop.
Google App Script (https://script.google.com/home) to automatically archive or delete emails. I use it to manage email from lists that I don't want to unsubscribe from but I want to prevent from building up forever in my inbox. Add a trigger to have it run every day. It emails me a summary of its actions, and it deletes old copies of that email!
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var totalCount = 0; | |
// Entry point must be named doGet for HTTP GET and doPost for HTTP POST | |
function doGet() { | |
return mailCleanup() | |
} | |
// Define mail filters, perform actions (archive or delete) on each one, send email report when done | |
function mailCleanup() { | |
var blastSenders = ['from:[email protected]', | |
'from:[email protected]', | |
'from:banana', | |
'((from:hamilton lottery AND unfortunately) OR subject:"lottery entry received")]; | |
var msg = 'Daily blasts older than 3 days:\n • ' + actOnMail('(' + blastSenders.join(' OR ') + ') AND in:inbox AND older_than:3d', 'delete') | |
msg += '\nSocial older than 30 days:\n • ' + actOnMail('category:social AND in:inbox AND older_than:30d', 'archive') | |
msg += '\nPromotions older than 30 days:\n • ' + actOnMail('category:promotions AND in:inbox AND older_than:30d', 'delete') | |
msg += '\nUpdates older than 14 days:\n • ' + actOnMail('category:updates AND in:inbox AND older_than:14d', 'archive') | |
GmailApp.sendEmail('[YOUR EMAIL ADDRESS HERE]', 'Mail Cleanup Report - ' + totalCount + ' messages cleaned', msg) | |
actOnMail('subject:"mail cleanup report" in:inbox older_than:5d', 'delete') // don't report on this one | |
Logger.log(msg) | |
return ContentService.createTextOutput(msg) | |
} | |
// Perform the actual gmail search and archive/delete resultset as requested | |
function actOnMail(filter, action) { | |
var maxThreads = 100 | |
var msgCount = 0 | |
while(true) { | |
var staleThreads = GmailApp.search(filter, 0, maxThreads) | |
msgCount += staleThreads.length | |
if(staleThreads.length > 0) { | |
Logger.log('Found ' + staleThreads.length.toString() + ' stale messages. Cleaning them now . . .') | |
var autoCleanedLabel = GmailApp.getUserLabelByName("autocleaned") | |
autoCleanedLabel.addToThreads(staleThreads) | |
switch(action) | |
{ | |
case 'archive': | |
GmailApp.moveThreadsToArchive(staleThreads) | |
break | |
case 'delete': | |
GmailApp.moveThreadsToTrash(staleThreads) | |
break | |
default: | |
Logger.log('Unknown action \'' + action + '\'! Ignoring it!') | |
break | |
} | |
} else { | |
break | |
} | |
} | |
totalCount += msgCount | |
var msg = msgCount.toString() + ' stale messages have been labeled \'autocleaned\' and ' + action + 'd.' | |
Logger.log(msg) | |
return msg | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment