Skip to content

Instantly share code, notes, and snippets.

@canadaduane
Last active February 11, 2025 00:37
Show Gist options
  • Save canadaduane/b5da111903ff748429bd425227af271c to your computer and use it in GitHub Desktop.
Save canadaduane/b5da111903ff748429bd425227af271c to your computer and use it in GitHub Desktop.

Revisions

  1. canadaduane revised this gist Sep 13, 2021. 1 changed file with 1 addition and 0 deletions.
    1 change: 1 addition & 0 deletions Filter-NGPVAN-Political-Emails.md
    Original file line number Diff line number Diff line change
    @@ -4,3 +4,4 @@
    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.
  2. canadaduane renamed this gist Sep 13, 2021. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  3. canadaduane revised this gist Sep 13, 2021. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions README.md
    Original file line number Diff line number Diff line change
    @@ -1,6 +1,6 @@
    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 above (copy/paste)
    4. Save
    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`
  4. canadaduane revised this gist Sep 13, 2021. 2 changed files with 6 additions and 1 deletion.
    6 changes: 6 additions & 0 deletions README.md
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,6 @@
    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 above (copy/paste)
    4. Save
    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`
    1 change: 0 additions & 1 deletion filterNGPVANSpam.js
    Original file line number Diff line number Diff line change
    @@ -15,7 +15,6 @@ function filterNGPVANSpam() {
    body.match(/^Received:.*ngp(van|web)\.com/m);

    if(matchedNGPVAN){
    console.log('message', matchedNGPVAN, body);
    GmailApp.moveThreadToSpam(threads[i]);
    }
    Utilities.sleep(500);
  5. canadaduane created this gist Sep 13, 2021.
    24 changes: 24 additions & 0 deletions filterNGPVANSpam.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,24 @@
    // 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){
    console.log('message', matchedNGPVAN, body);
    GmailApp.moveThreadToSpam(threads[i]);
    }
    Utilities.sleep(500);
    }
    }
    }