Skip to content

Instantly share code, notes, and snippets.

@Meandmybadself
Last active September 27, 2022 16:23
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 Meandmybadself/5759496b37da8196cbe170d28c19c127 to your computer and use it in GitHub Desktop.
Save Meandmybadself/5759496b37da8196cbe170d28c19c127 to your computer and use it in GitHub Desktop.
Mark NGPVan emails as spam
function filterNGPVANSpam() {
// Look for emails containing "unsubscribe" sent in the past week.
// This'll be more than just NGPVAN emails. We'll look in their headers below.
const lastWeek = new Date((new Date()).getTime() - 604800000)
const lastWeekStr = lastWeek.getFullYear() + '/' + (lastWeek.getMonth()+1) + '/' + lastWeek.getDate()
// The gmail query "unsubscribe after: DATE"
const query = 'unsubscribe after:' + lastWeekStr
// Search for any email thread that meets this criteria.
const threads = GmailApp.search(query)
for(let i=0; i < threads.length; i++) {
// Get the messages for this thread.
const messages = threads[i].getMessages()
let threadIsSpam = false
for(let j=0; (!threadIsSpam && j < messages.length); j++) {
const message = messages[j];
const body = message.getRawContent();
const matchedNGPVAN =
body.match(/^List-Unsubscribe:\s*<(.*ngpvan\.com.*)>\s*$/m) || // If the unsubscribe points back to spam source or
body.match(/^Received:.*ngp(van|web)\.com/m); // the Received header points back to spam source.
if(matchedNGPVAN){
console.log('Found NGPVAN content. Moving thread to spam')
console.log('From: ' + message.getFrom())
console.log('Subject: ' + message.getSubject())
threadIsSpam = true
GmailApp.moveThreadToSpam(threads[i]);
GmailApp.markThreadRead(threads[i]);
}
Utilities.sleep(500); // Wait 500 seconds.
}
}
}

Mark NGPVAN emails as spam & read

(Inspired by https://gist.github.com/canadaduane/b5da111903ff748429bd425227af271c)

  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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment