Created
June 7, 2018 17:06
-
-
Save jernejcic/bef53451d8440d83f3de01839d213a32 to your computer and use it in GitHub Desktop.
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
/* | |
* IMPORTANT: THIS FILE SHOULD NEVER BE SHARED AS IT COULD POTENTIALLY | |
* BE A MAJOR SECURITY HOLE, ALLOWING SOMEONE WITH ACCESS THE | |
* ABILITY TO MANIPULATE AND SEE ANYTHING INSIDE OF GMAIL | |
* | |
* BY: LUKE RYAN JERNEJCIC | |
* WEBSITE: https://lukeryan.org | |
* LICENSE: GPLv3 | |
* | |
* This is a very dangerous file. Any gmail filter in here is used to delete | |
* all of the matching emails. To add some safeguards, the filter only acts | |
* on messages inside the inbox, and the filter must have an `older_than:` | |
* filter on it to help enforce intentionality. | |
* | |
* The filters should never be intended to act on incoming mail. You can use | |
* Gmail's built in functionality. The purpose of these filters are to delete | |
* messages that are of interest, but start clogging up the inbox over time. | |
* For example, newsletters that you're interested in, but don't always get | |
* time for and don't need requiring viewing. | |
*/ | |
var emailLogsTo = 'test@test.com'; | |
var filterForInsideCom = 'label:inbox {from:x@x.x from:r@r.r} older_than:12h'; | |
function deleteEmailsForFilter(filter) { | |
Logger.log('>> START Processing deletion of emails using filter `' + filter + '`'); | |
if (typeof filter !== 'string' || filter.length < 10) { | |
Logger.log('Bad filter received. Aborting deletion of emails'); | |
return -1; | |
} | |
if (filter.indexOf('label:inbox') < 0) { | |
Logger.log('Bad filter received. Will only delete emails from inbox. This is all for some overlab in time for the user to see the message.'); | |
return -1; | |
} | |
if (filter.indexOf('older_than:') < 0) { | |
Logger.log('Bad filter received. Requires filter to specify an `older_than:` filter to allow for the user time to see emails.'); | |
return -1; | |
} | |
var emails = GmailApp.search(filter); | |
Logger.log('Found ' + emails.length + ' emails'); | |
for (var i = 0; i < emails.length; i++) { | |
var e = emails[i]; | |
var messages = e.getMessages(); | |
var desc = 'from "'+ e.getMessages()[0].getFrom() + '" with subject "' + e.getFirstMessageSubject() + '"'; | |
if (messages.length > 1) { | |
Logger.log('Won\'t delete threads with more than 1 message. Skipping ' + desc); | |
} else { | |
Logger.log('Moving email to trash ' + desc); | |
e.moveToTrash(); | |
} | |
} | |
Logger.log('<< COMPLETED'); | |
return emails.length; | |
} | |
function deleteEmailsForFilters(filters) { | |
var count = 0; | |
if (typeof filters !== 'object' || filters.constructor.name !== Array.prototype.constructor.name) { | |
Logger.log('Must pass in an array of filters.'); | |
return -1; | |
} | |
for (var i = 0; i < filters.length; i++) { | |
var c = deleteEmailsForFilter(filters[i]); | |
if (c > 0) { | |
count += c | |
} else if (c < 0) { | |
return -1; | |
} | |
} | |
return count; | |
} | |
var lineStyle = 'margin: 5px 0; background: rgb(249,249,249); background: linear-gradient(135deg, rgba(249,249,249,1) 0%, rgba(188,244,255,1) 100%);'; | |
function run() { | |
var emailsProcessed = deleteEmailsForFilters([filterForInsideCom]); | |
Logger.log('Processed %s emails.', emailsProcessed); | |
if (emailsProcessed !== 0) { | |
Logger.log('Sending notification email.'); | |
var subject = '[App Script] Auto deleted '+ emailsProcessed + ' emails'; | |
var body = HtmlService | |
.createHtmlOutput('<div style="' + lineStyle + '">' + Logger.getLog().replace(/[\n\r]/g, '</div><div style="' + lineStyle + '">') + '</div>') | |
.getContent(); | |
MailApp.sendEmail(emailLogsTo, subject, Logger.getLog(), {htmlBody: body}); | |
Logger.log('Completed sending email.'); | |
} else { | |
Logger.log('Nothing to report. Move along...'); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment