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:
- 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.
- 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.
- 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".
- 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).
- 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.
- 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.
- 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());
}
}
}
}