Question

I am so sick of getting unwanted mails in my gmail inbox that I am now willing to create a white-list kind of extension which filters all mails coming from people who are not in my contacts list. I searched for this many hours but could not find anything hence thinking of doing this exercise (if it exists, please share the link). I have created 100's of filters but definitely spammers outpace me everytime.

Can someone tell me whether this is possible in first place? I have seen extensions which add functionality in gmail but I don't know how to block an email through an extension. Plz help.

Was it helpful?

Solution

You can setup a whitelist in Gmail but it is unlikely to work for such a large list of addresses. What you can do is create a Google sheet with a list of valid addresses and a Google Script that will scan your inbox against these addresses.

var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheets()[0];

var range = sheet.getDataRange();
var values = range.getValues();

var emails = [];

for (var i in values) {
  emails.push(values[i][0]);
 }

 var threads = GmailApp.search("in:inbox is:unread");
 for (var i=0; i<threads.length; i++) {
   var from = threads[i].getMessages()[0].getFrom();
   if ( !emails.indexOf(from) ) {
      threads[i].moveToSpam();
    }
  }

You need to setup a trigger that runs this script every 5 minutes or so.

OTHER TIPS

Thanks a lot Amit for sharing this snippet. With the help of this, I was able to come up with a working solution (ver1.0) and sharing below for others:

function createTriggers() {

  ScriptApp.newTrigger('runSpamFilter').timeBased().everyMinutes(10).create();

  SpreadsheetApp.getActiveSpreadsheet().toast("The program will check for spam email every 10 minutes and"
         + " send them to Spam Folder after applying label MySpam. You may please close this window.", "Initialized");

}

function removeTriggers(show) {    

  var triggers = ScriptApp.getScriptTriggers();

  for (i=0; i<triggers.length; i++) {
    ScriptApp.deleteTrigger(triggers[i]);
  }  

  if (show) {
    SpreadsheetApp.getActiveSpreadsheet().toast("The program has stopped.", "Uninstalled");
  }

}

function runSpamFilter() {

  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var sheet = ss.getSheets()[0];

  var range = sheet.getDataRange();
  var values = range.getValues();

  var emails = [];
  var regex = /([a-zA-Z0-9+._-]+@[a-zA-Z0-9._-]+\.[a-zA-Z0-9._-]+)/gi;  

  var my_label = GmailApp.getUserLabelByName("MySpam");
  var spamCount = 0;

  for (var i in values) {
    emails.push(values[i][0]);
  }

  var threads = GmailApp.search("in:inbox is:unread");

  for (var i=0; i<threads.length; i++) {
    var from = threads[i].getMessages()[0].getFrom();
    var from_email = from.match(regex);

    if ( emails.indexOf(from_email[0]) == -1 ) {
      threads[i].addLabel(my_label);
      threads[i].moveToSpam();
      spamCount++;
    }
  }

  Logger.log("Spams found = %s", spamCount);

}

function startProgram() {    

  removeTriggers(false);
  createTriggers();

}

function onOpen() {  

  var sheet = SpreadsheetApp.getActiveSpreadsheet();

  var menu = [ 
    {name: "Step 1: Initialize", functionName: "startProgram"},
    {name: "Step 2: Start ", functionName: "runSpamFilter"},
    {name: "Uninstall (Stop)", functionName: "removeTriggers"}
  ];  

  sheet.addMenu("Gmail Spam Filter v1.0", menu);    
}

I may also come up with ver2.0 which removes the current limitation of this script. As of now, you have to make a spreadsheet having all your contacts email addresses. But once you add a new contact, this spreadsheet needs to be updated manually. Hence this script needs an additional trigger which would update the spreadsheet once in say 15 days with the recently added contacts/email addresses. I will share that too later or may be someone can pick from here and come up with ver2.0.

Thanks again.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top