https://github.com/pasanlaksitha/iit-gmail-microsite-filter
A Gmail filter script for IIT students to automatically label and archive emails containing sites.google.com links.
https://github.com/pasanlaksitha/iit-gmail-microsite-filter
apps-script email-filter gmail iit spam-filtering
Last synced: 5 months ago
JSON representation
A Gmail filter script for IIT students to automatically label and archive emails containing sites.google.com links.
- Host: GitHub
- URL: https://github.com/pasanlaksitha/iit-gmail-microsite-filter
- Owner: Pasanlaksitha
- Created: 2025-03-31T04:03:43.000Z (11 months ago)
- Default Branch: master
- Last Pushed: 2025-07-31T07:44:15.000Z (6 months ago)
- Last Synced: 2025-07-31T11:19:04.386Z (6 months ago)
- Topics: apps-script, email-filter, gmail, iit, spam-filtering
- Language: JavaScript
- Homepage:
- Size: 1.87 MB
- Stars: 7
- Watchers: 1
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# 📧 IIT Gmail Microsite SPAM Filter (Google Apps Script)


This Google Apps Script filters your Gmail Inbox to find emails that contain links to `sites.google.com`, even if they are **masked behind custom text** like "Click Here". It then:
- Labels those emails under a folder called **Microsite**
- Archives them to remove from the **Inbox**
- Keeps your inbox clean from unwanted promotional website links
---
## 🎓 Who Is This For?
This script is made specifically for **IIT (Institute of Information Technology)** students who want to automatically filter out emails that promote personal websites using Google Sites. No coding knowledge is required — just copy, paste, and set it up!
## 🚀 What It Does
✅ Detects:
- Visible links like `https://sites.google.com/...`
- Masked links like `[Click Here](https://sites.google.com/view/project/home)`
- Raw URLs embedded anywhere in email content
✅ Automatically:
- Labels the email as `Microsite`
- Moves it out of the Inbox (archived)
- Runs on all Inbox emails received in the last **30 days**
---
## 🛠️ How to Install and Use
> You don't need to deploy anything. Just follow the steps below.
---
### 🔧 Step 1: Open Google Apps Script
1. Go to [https://script.google.com](https://script.google.com)
2. Click **`+ New project`**
3. Name your project (e.g., `Microsite Link Filter`)
---
### 📋 Step 2: Copy and Paste the Script
Replace the default `Code.gs` content with the code below:
```javascript
function detectAllGoogleSiteLinks() {
const threads = GmailApp.search("in:inbox newer_than:30d"); // configure the dates you need to scrap
const label = GmailApp.createLabel("Microsite"); // Label Folder change if desired
threads.forEach((thread) => {
const messages = thread.getMessages();
messages.forEach((message) => {
const htmlBody = message.getBody();
// Check direct plain-text match
if (htmlBody.includes("sites.google.com")) {
applyMicrositeLabelAndArchive(thread, label);
Logger.log(
"Found plain sites.google.com link in: " + message.getSubject()
);
return;
}
// Check masked links
const hrefRegex =
/]*?href=["'](https:\/\/sites\.google\.com\/[^"']+)["'][^>]*?>.*?<\/a>/gi;
let match;
while ((match = hrefRegex.exec(htmlBody)) !== null) {
const foundUrl = match[1];
applyMicrositeLabelAndArchive(thread, label);
Logger.log(
"Found masked link: " + foundUrl + " in: " + message.getSubject()
);
return;
}
// Fallback: check raw URLs in the body
const rawLinkRegex = /(https:\/\/sites\.google\.com\/[^\s"'<>]+)/gi;
const rawMatches = htmlBody.match(rawLinkRegex);
if (rawMatches && rawMatches.length > 0) {
applyMicrositeLabelAndArchive(thread, label);
Logger.log(
"Found raw link(s): " +
rawMatches.join(", ") +
" in: " +
message.getSubject()
);
return;
}
});
});
}
// Label thread and remove from Inbox
function applyMicrositeLabelAndArchive(thread, label) {
const existingLabels = thread.getLabels().map((l) => l.getName());
if (!existingLabels.includes(label.getName())) {
thread.addLabel(label);
}
thread.moveToArchive(); // removes it from Inbox
}
```
---
### ▶️ Step 3: Run the Script Once
1. Click the `▶️ Run` button (in the top toolbar)
2. It will prompt you for authorization — allow permissions for Gmail
3. The script will scan your Inbox for `sites.google.com` links and start filtering

## 
### ⏰ Step 4: Schedule It to Run Automatically (Optional)
1. In the Apps Script editor, click the **clock icon** (Triggers)
2. Click `+ Add Trigger`
3. Configure:
- Function: `detectAllGoogleSiteLinks`
- Event source: `Time-driven`
- Type: `Day timer`
- Time of day: e.g., `7:00am to 8:00am`
This will automatically run the filter every morning.

---
## 📂 Where Do Filtered Emails Go?
- They are moved out of your Inbox
- You can find them under the **`Microsite`** label in Gmail
## 
## 📌 Notes
- You can change `"Microsite"` to any label name you prefer
- You can update `newer_than:30d` to scan a different time range (like `10d` or `1d`)
- This script works only for the emails in your own Gmail account
---
## ❌ To Stop It
If you no longer want to run the filter:
- Go to the **Triggers** tab and delete the trigger
- Or just delete the script project from your Apps Script dashboard
---
## ✅ Requirements
- your IIT Gmail Account eg: `spammer.2023233@iit.ac.lk`
---