Skip to content

Instantly share code, notes, and snippets.

@RobertLowe
Last active January 8, 2022 18:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save RobertLowe/bfcba6bee8b590eb14f2d5b930061795 to your computer and use it in GitHub Desktop.
Save RobertLowe/bfcba6bee8b590eb14f2d5b930061795 to your computer and use it in GitHub Desktop.
Anti Newsletter Script for Gmail (and Archive)
function processInbox() {
var label = GmailApp.getUserLabelByName("Newsletter");
if(!label){
label = GmailApp.createLabel("Newsletter");
}
// process all recent threads in the Inbox
var threads = GmailApp.search("newer_than:2d -minusimportantthings");
for (var i = 0; i < threads.length; i++) {
// get all messages in a given thread
var messages = threads[i].getMessages();
var thread = threads[i];
for (var j = 0; j < messages.length; j++) {
var message = messages[j];
var body = message.getRawContent();
// any email with unsubscribe in the email header OR message content
if ((body.indexOf("Unsubscribe") > -1) || body.indexOf("unsubscribe") > -1) {
thread.addLabel(label)
thread.moveToArchive();
thread.markRead();
}
}
}
}
@RobertLowe
Copy link
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment