Gmail – way to search/filter gmail emails by the absolute domain without subdomains (e.g. google.com, but not plus.google.com)

gmailgmail-filtersgmail-search

Is there a way to search/filter gmail emails by the absolute domain without subdomains? If just do a search/filter on from:@google.com it includes all subdomains like plus.google.com, txt.voice.google.com, etc.

I know I can manually exclude all of those with -{from:plus.google.com from:txt.voice.google.com}, but the list of subdomains is quite long (and I don't really know the full list).

Best Answer

What you can use is a Google Script that does regex match with the TO field to find emails with an exact domain in the address:

function myFunction() {

  var label = GmailApp.getUserLabelByName("Google.com");
  if (!label) label = GmailApp.createLabel("Google.com");

  var threads = GmailApp.search("from:google.com");
  for (var t in threads) {
    var msg = threads[t].getMessages()[0];
    if (msg.getTo().match(/\@google\.com/)) {
      threads[t].addLabel(label);
    }
  }

}

Also see: Advanced Gmail Search with RegEx