Skip to content

Instantly share code, notes, and snippets.

@crates
Last active May 30, 2024 19:48
Show Gist options
  • Save crates/230b1e10a07424ab2eba87f6199b966d to your computer and use it in GitHub Desktop.
Save crates/230b1e10a07424ab2eba87f6199b966d to your computer and use it in GitHub Desktop.
Filter out "via" spam from Gmail (using script.google.com)
function processMessages(thread, label, search) {
var messages = thread.getMessages();
for (var j = 0; j < messages.length; j++) {
var message = messages[j];
var body = message.getRawContent();
if (typeof search === 'string') { // used for single-string searches:
if (body.indexOf(search) > -1) {
thread.addLabel(label).markRead().markUnimportant().moveToArchive();
break;
}
} else if (Array.isArray(search)) { // search for multiple addresses:
for (var k = 0; k < search.length; k++) {
if (body.indexOf(search[k]) >- 1) {
thread.addLabel(label).markRead().markUnimportant().moveToArchive();
break;
}
Utilities.sleep(50);
}
}
Utilities.sleep(1000);
}// for
}// processMessages(thread, label, search)
function processRecruiters(thread) {// filters all threads in the Inbox for spammy recruiters
var label = GmailApp.getUserLabelByName('# Freelancing Leads/Awful Recruiters');
var viaAddresses = [
'email.bullhornmail.com',
'ceipal.com',
'conrepbkmail.com',
'sm-conrep.com',
'conrep-smtp.com',
'consmtp.com',
'ziprecruiter.com',
'conrepbk.com',
'joboppforyou.com',
'conrepms.com',
'bkconrep.com',
'amigainformatics.net',
'amigainformatics.com',
'mmconrep.com',
'jobdivabk.com',
'mg.compqsoft.com'
];
processMessages(thread, label, viaAddresses);
}// processRecruiters()
function processSwingShoes(thread) {// filters all threads in the Inbox for SwingShoes mailing list
var label = GmailApp.getUserLabelByName('# Hobbies/Dance Classes');
processMessages(thread, label, 'swingshoes.ccsend.com');
}// processSwingShoes()
function processJobDiva(thread) {// filters all threads in the Inbox for JobDivaBK
var label = GmailApp.getUserLabelByName('# Freelancing Leads/Awful Recruiters');
processMessages(thread, label, 'Received: from jobdivabk.com');
}// processJobDiva()
function filterViaSpam() {
var threads = GmailApp.getInboxThreads(0, 10);
for (var i = 0; i < threads.length; i++) {
var thread = threads[i];
processRecruiters(thread);
processSwingShoes(thread);
processJobDiva(thread);
} // for
}
@crates
Copy link
Author

crates commented Aug 20, 2019

Be sure to check out the tutorial that inspired this script:
https://www.geektron.com/2014/01/how-to-filter-gmail-using-email-headers-and-stop-via-spam/

@lcreed
Copy link

lcreed commented Feb 1, 2023

Hey @crates - do you have a new version of this script? I have a ton of job recruiter spam using jobopp domain and google's filters just aren't cutting it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment