Skip to content

Instantly share code, notes, and snippets.

@johnie
Last active January 25, 2024 16:23
Show Gist options
  • Save johnie/f7d9db207a5c8c0a8fa85129c4049556 to your computer and use it in GitHub Desktop.
Save johnie/f7d9db207a5c8c0a8fa85129c4049556 to your computer and use it in GitHub Desktop.
Filter Phish – This Google Apps Scripts filter will automatically move all future emails with the header `X-PHISHTEST` to trash.

Filter Phish

This Google Apps Scripts filter will automatically move all future emails with the header X-PHISHTEST to trash.

Getting started

  1. Go to Google Apps Script
  2. Add a new script and copy paste the content from the sibling file
  3. Test run the script, go to View in the menu bar and click Logs
    • If there's no errors it should be blank, OR a verbose message that it removed a phishing email.

Make it run every hour

If you want to run the script every hour, click this button button and enable the script.

Done!

/*
This function is meant to run on a time-based trigger (I have it set for every 1 hour).
The filter will automatically move all future emails with the header `X-PHISHTEST` to trash.
For this function to run correctly you must enable the Advanced Gmail Service
see: https://developers.google.com/apps-script/advanced/gmail
*/
function filterPhish() {
try {
// Get the most recent 50 threads in your inbox
var threads = GmailApp.search("in:inbox", 0, 50);
// If there are threads
if (threads.length > 0) {
// For each thread
for (var t=threads.length-1; t>=0; t--) {
// Get the current thread we are iterating over
var thread = threads[t];
// Get the first message in the thread
var message = thread.getMessages()[0];
// Get the from address
var from = message.getFrom();
// Get the sender's raw content
var content = message.getRawContent();
// See if the raw content has the header `X-PHISHTEST`
if (content.indexOf("X-PHISHTEST") > -1) {
// Log that the filter was created
Logger.log("Filter phishing " + message.getDate() + "::" + message.getSubject() + " :: " + from);
// Mark the thread as trash it
thread.moveToTrash();
}
}
}
} catch (e) {
Logger.log(e.toString());
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment