4

I'm being spammed a lot lately from *@*.in domains I know how to create a filter so I can auto-delete those messages. But I need a search that only searches in the mail address.

for example, if I use from:*.in I do get all the *@*.in emails but I also get emails where the sender name contains in.

How do I exclude the sender name from the search? Or search only in the email address?

4

This is a result of Gmail ignoring periods (".") in email addresses, a function. You may need to build a filter targeting all the email addresses from whom you receive the offending messages:

from:spam@linked.in OR spam@registry.in OR ...

A pain, I know, but given Google is unlikely to change how the period is interpreted, it may be your only option.

  • yeah if it only was that easy :( they are using a different domain name for every email they send. so adding them to a spam list doesn't help. – FLY Jun 20 '12 at 8:03
  • 1
    Sorry! Your best option may just be to use the 'Report Spam' button in Gmail and let it learn that you hate all '.in' domains. – Jordan Finnigan Jun 20 '12 at 12:18
  • No worries :) I'm already doing that just hoped there was a better solution. I would give you a +1 for trying but my rep on this site is still 1 so I can't upvote yet – FLY Jun 20 '12 at 13:36
1

For a filter, I used the following:

from:(host.com OR email@otherhost.com)

I hope that helps shed a light. I know it took me a while to figure out that Google was using their same search operators in Gmail.

  • As explained in the comments of the answer of JadoJodo the domain names differ so it won't be possible to put in every host name. I just need a wildcard for the domain name. – FLY Sep 30 '13 at 7:48
1

You could use a Google Apps Script to isolate the TLD and take action.

In my case, Gmail does a great job of catching the emails as SPAM, but since I don't want to ever see the messages from many TLDs, I wrote this script to have those messages permanently deleted.

function scanSpam() {
  // Locate spam TLDs file in Google Drive
  var fileName = "spam_tlds.txt";
  var files = DriveApp.getFilesByName(fileName);

  // Report if nothing found
  if (!files.hasNext()) {
    throw new Error("Cannot find " + fileName + ".");
  }

  // Read into array text of first/only file found
  var file = files.next();
  var text = file.getBlob().getDataAsString("utf8");
  var tlds = text.split(/\r?\n/);

  // Search Spam folder
  var threads = GmailApp.search("label:spam");

  // Loop for each thread
  for (var i = 0; i < threads.length; i++) {
    // Parse messages
    var messages = threads[i].getMessages();

    // Loop for each message in thread
    for (var j = 0; j < messages.length; j++) {
      // Parse each message header and extract tld
      var message = messages[j];
      var from = message.getFrom();
      var tld = from.substring(from.lastIndexOf(".")).replace(/[^0-9a-z.]/gi, "");

      // Delete message if tld is in tlds list
      if (tlds.indexOf(tld) !== -1) {
         // GmailApp.moveMessageToTrash(message);
         Gmail.Users.Messages.remove("me", messages[j].getId());
      }
    }
  }
}

Here's the process:

  1. Add a new script to https://script.google.com, making sure that you are logged into Google with the same account you want to filter with GMail. When you attempt to run the script for the first time, navigate through the warning messages (using the Advanced section) to let the script run.
  2. Create a list of the unwanted TLDs in a text document, and store it in Google Drive with the name "spam_tlds.txt". If you use a different name, be sure to change the var fileName entry in the script to match the new name. Also be sure the file does not get converted into a Google Doc — it needs to remain as plain text. Inside the file, put each TLD on its own line.
  3. When you run the script, it searches Google Drive for the file, reads the first file it finds by that name, takes each line (TLD), and adds it to an array (a list in memory) called "tlds".
  4. After all of the TLDs are read in, the script walks through the Spam folder, looking for each message thread, and each message within the thread. For each message it finds, it parses the "From" field and strips it down to anything after the last period (i.e., the TLD).
  5. Each message TLD is compared with the list (array) of TLDs found in the text file of TLDs. If there's a match, the message is permanently deleted (bypassing the Trash folder). In order to get that to work, you need to follow the instructions provided in this stackoverflow answer: https://stackoverflow.com/a/37799896/199374.
  6. Note that if you just want the message moved to Trash, you can remove the comment // from GmailApp.moveMessageToTrash(message); line of the script and either move the // comment notation to the next line or remove the second line altogether. If you do this (that is, you only want to move the file to Trash), you won't need to go through the steps in the above cited stackoverflow link.
  7. After you have tested your script, set a "trigger" that causes it to run at whatever increment (e.g., every 5 minutes) you desire. To the best of my knowledge, there's no other way to trigger the script (e.g., by the arrival of new mail). Note that the Gmail folder display isn't always up-to-date. When testing your script, you might have to refresh the SPAM folder to see the real content. Likewise, since you're going to have the script running at whatever increment you choose, there's a chance that you might open your SPAM folder prior to the automatic trigger causing the script to purge the unwanted messages.

You can easily extend this script to include other criteria based on sending details, content in the body of the message, etc.


UPDATE

Here's an enhanced version of the script that also checks the pretty-email address against a list of spam senders, plus checks for spam words in the subject line. As you'll see, the subject line test is a bit more complicated because it has to independently compare each word in the subject versus the entire subject line. It also strips out everything except alpha characters so that it doesn't fall victim to various tricks like "vibra8tor" and "vibra****tor" versus "vibrator". Your three files, containing the criteria for flagging the spam, should be titled "spam_tlds.txt", "spam_senders.txt", and "spam_subjects.txt".

function scanSpam() {
    // List criteria files
  var lists = ["tlds", "senders", "subjects"];

  // Define variables
  var criteria = [];
  var filesNames, files, file, text;

  // Locate and parse files
  for (var i = 0; i < lists.length; i++) {
    // Locate files in Google Drive
    fileName = "spam_" + lists[i] + ".txt";
    files = DriveApp.getFilesByName(fileName);

    // Report if nothing found
    if (!files.hasNext()) {
      throw new Error("Cannot find " + fileName + ".");
    }

    // Read into array text of first/only specified file name found
    file = files.next();
    text = file.getBlob().getDataAsString("utf8");

    // Save in array
    if (lists[i] !== "subjects") {
      criteria[i] = text.toLowerCase().split(/\r?\n/);
    }

    // Strip numerics and symbols, save in array, and create regEx
    else {
      criteria[i] = text.replace(/[^a-z\r\n]/gi, "").toLowerCase().split(/\r?\n/);
      var subjects = new RegExp(criteria[i].join("|"), "g");
    }
  }

  // Search Spam folder
  var threads = GmailApp.search("label:spam");

  // Loop for each thread
  for (var i = 0; i < threads.length; i++) {
    // Parse messages
    var messages = threads[i].getMessages();

    // Loop for each message in thread
    for (var j = 0; j < messages.length; j++) {
      // Parse each message header
      var message = messages[j];
      var subject = message.getSubject();
      var from = message.getFrom();
      var formattedFrom = from.indexOf("<") > 1 ? from.substring(0, from.indexOf("<") -1).replace(/[^0-9 a-z.-]/gi, "").toLowerCase().trim() : "";
      var tld = from.substring(from.lastIndexOf(".")).replace(/[^0-9a-z.]/gi, "");

      // Examine every word in subject
      var hasSpamTLD = tld.trim() === "" || criteria[0].indexOf(tld) !== -1;
      var hasSpamFrom = formattedFrom.trim() === "" || criteria[1].indexOf(formattedFrom) !== -1;
      var hasSpamSubject = (! new RegExp("^[,\,]+$").test(subject.replace(/[^a-z,]/gi, "").toLowerCase().match(subjects)));

      // Permanently delete violations
      if (hasSpamTLD || hasSpamFrom || hasSpamSubject) {
        Gmail.Users.Messages.remove("me", messages[j].getId());
      }
    }
  }
}
0

I know this is an old thread, but my reply might help anyone who comes across it.

FLY said:

for example, if I use from:*.in I do get all the *@*.in emails but I also get emails where the sender name contains in.

That's because Gmail ignores non-alphanumeric characters, not just . but also * and @. So, in the example you give, from:*.in is simplified to from:in by Google. In other words, Google is returning exactly what you are requesting.

There is a way to define the filter you want, though it is tedious for long lists of domains. For simplicity, let's assume that you want to filter 5 domains, viz:

  • aaa.in
  • bbb.in
  • ccc.in
  • ddd-in
  • eee.in

The filter from:"{aaa bbb ccc ddd eee} AROUND 1 com" will achieve the desired effect, including all sub-domain levels and all senders.

In my own inbox, for example, using the filter from:"{adobe google microsoft} AROUND 1 com" returns email messages sent from the following list of email addresses:

  • account-security-noreply@account.microsoft.com
  • assistant-noreply@google.com
  • email@mail.adobesystems.com
  • forums_noreply@adobe.com
  • googlenews-noreply@google.com
  • googleplay-noreply@google.com
  • mail@info.adobesystems.com
  • mail-noreply@google.com
  • message@adobe.com
  • no-reply@accounts.google.com
  • noreply-278f7a05@plus.google.com
  • noreply-e2a903f5@plus.google.com
  • noreply+feedproxy@google.com
  • noreply-google-plus@google.com
  • noreply-inactive-account-manager@accounts.google.com
  • noreply-local-guides@google.com
  • noreply-photos@google.com
  • panoramio-noreply@google.com
  • privacy-noreply@google.com
  • privacy-noreply@policies.google.com

See https://support.google.com/mail/answer/7190?hl=en for details of search operators you can use with Gmail.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.