- Go to https://script.google.com
- Create a New Project
- Replace the
Code.gs
file it creates for you with the javascript below (copy/paste) - Save the script
- Go to Triggers (looks like an alarm clock on left-hand side)
- Create a Trigger that acts every 10 minutes and calls
filterNGPVANSpam
- 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.
Last active
February 6, 2024 18:16
-
-
Save canadaduane/b5da111903ff748429bd425227af271c to your computer and use it in GitHub Desktop.
Move Incoming NGPVAN Political Emails in Gmail to Spam (Google Apps Script)
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
// 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); | |
} | |
} | |
} |
Thank you for this. For some reason this script didn't work for me at first. It just gave an error about function name before getting to the authentication part. To fix it, I added a line break after the opening parenthesis on line 4, so that my new line 5 only had that opening parenthesis on it, with everything else moved down one line. Then it worked fine.
By the way, for anyone that doesn't know what NGPVAN is, they are the email system that most of the Democratic candidates use. When you try to unsubscribe from their list, you only get removed from that one candidate's list, but not the master NGPVAN list. So you have to unsubscribe to every candidate that comes along, one at a time, forever. :(
thank you!
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
THANK YOU!