Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save jonathanhle/0425c6118b7755c1a99d80a451050c4e to your computer and use it in GitHub Desktop.
Save jonathanhle/0425c6118b7755c1a99d80a451050c4e to your computer and use it in GitHub Desktop.
Move Incoming NGPVAN Political Emails in Gmail to Spam (Google Apps Script)
  1. Go to https://script.google.com
  2. Create a New Project
  3. Replace the Code.gs file it creates for you with the javascript below (copy/paste)
  4. Save the script
  5. Go to Triggers (looks like an alarm clock on left-hand side)
  6. Create a Trigger that acts every 10 minutes and calls filterNGPVANSpam
  7. You'll need to authorize this script to act on your behalf, which may require that you use the scary "Advanced" section to allow the script to read/write to your email inbox.
// Adapted with thanks from:
// https://www.geektron.com/2014/01/how-to-filter-gmail-using-email-headers-and-stop-via-spam/
function filterNGPVANSpam() {
var threads = GmailApp.getInboxThreads(0, 5);
threads.concat(GmailApp.search('category:promotions', 0, 5));
for (var i = 0; i < threads.length; i++) {
var messages = threads[i].getMessages();
for (var j = 0; j < messages.length; j++) {
var message = messages[j];
var body = message.getRawContent();
var matchedNGPVAN =
body.match(/^List-Unsubscribe:\s*<(.*ngpvan\.com.*)>\s*$/m) ||
body.match(/^Received:.*ngp(van|web)\.com/m);
if(matchedNGPVAN){
GmailApp.moveThreadToSpam(threads[i]);
}
Utilities.sleep(500);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment