🔍 Overview
The Mail Tracker utilizes the Google Apps Script system, which allows creating scripts for automating tasks in Google Workspace. In this case, the script is designed to monitor incoming emails and detect domain blocks.
⚙️ Features
- Monitoring incoming emails every 15 minutes
- Detecting domain blocks
- Sending blocked domains to Link Switcher API
- Notifying users about blocked domains
- Automatic switch-to-backup for blocked domains
🛠️ Implementation Details
The script is written in JavaScript and runs in the Google Apps Script environment. It uses the following Google services:
- GmailApp: To access and manage Gmail messages.
- UrlFetchApp: To make HTTP requests to external APIs.
🧩 Script
function processNewEmails() {
var now = new Date();
var fiveMinutesAgo = new Date(now.getTime() - 15 * 60 * 1000);
var query = "after:" + formatDateForGmail(fiveMinutesAgo);
var threads = GmailApp.search(query);
var blockedDomains = [];
threads.forEach(function(thread) {
var messages = thread.getMessages();
messages.forEach(function(message) {
if (message.getDate() >= fiveMinutesAgo) {
if (message.getSubject() === "Social engineering content detected on") {
var domain = extractDomainFromText(message.getPlainBody());
if (domain) {
blockedDomains.push(domain);
}
}
}
});
});
if (blockedDomains.length > 0) {
Logger.log("Sending blocked domains: " + String(blockedDomains));
sendDomainsToProduction(blockedDomains);
sendDomainsToStaging(blockedDomains);
sendDomainsToDevelop(blockedDomains);
} else {
Logger.log("Not domains found in the last 15 minutes.");
}
}
function extractDomainFromText(text) {
var match = text.match(/detected on\s+([\w.-]+\.[a-z]{2,})/i);
if (match) {
return match[1];
}
return null;
}
function sendDomainsToProduction(blockedDomains) {
var url = "https://link-switcher-master.almightypush.com/api/v1/telegram/blocked-by-google-console/";
var payload = {
domains: blockedDomains,
account: "stgnashpush@gmail.com"
};
var options = {
method: "post",
contentType: "application/json",
headers: {
"Authorization": "Bearer ***"
},
payload: JSON.stringify(payload),
muteHttpExceptions: true
};
var response = UrlFetchApp.fetch(url, options);
Logger.log("Response (prd): " + response.getResponseCode() + " " + response.getContentText());
}
function sendDomainsToStaging(blockedDomains) {
var url = "https://link-switcher-staging.almightypush.com/api/v1/telegram/blocked-by-google-console/";
var payload = {
domains: blockedDomains,
account: "stgnashpush@gmail.com"
};
var options = {
method: "post",
contentType: "application/json",
headers: {
"Authorization": "Bearer ***"
},
payload: JSON.stringify(payload),
muteHttpExceptions: true
};
var response = UrlFetchApp.fetch(url, options);
Logger.log("Response (stg): " + response.getResponseCode() + " " + response.getContentText());
}
function sendDomainsToDevelop(blockedDomains) {
var url = "https://link-switcher-develop.almightypush.com/api/v1/telegram/blocked-by-google-console/";
var payload = {
domains: blockedDomains,
account: "stgnashpush@gmail.com"
};
var options = {
method: "post",
contentType: "application/json",
headers: {
"Authorization": "Bearer ***"
},
payload: JSON.stringify(payload),
muteHttpExceptions: true
};
var response = UrlFetchApp.fetch(url, options);
Logger.log("Response (dev): " + response.getResponseCode() + " " + response.getContentText());
}
function formatDateForGmail(date) {
var year = date.getFullYear();
var month = ("0" + (date.getMonth() + 1)).slice(-2);
var day = ("0" + date.getDate()).slice(-2);
return year + "/" + month + "/" + day;
}⚙️ Setting Up
- Open Google Apps Script.
- Create a new project and paste the script above.
- Set up a time-driven trigger to run the
processNewEmailsfunction every 15 minutes. - Replace the placeholder
Bearer ***with your actual API tokens for production, staging, and development environments. - Save and authorize the script to access your Gmail account.
🔗 API Interaction
The script interacts with the following API endpoint:
https://link-switcher-{environment}.almightypush.com/api/v1/telegram/blocked-by-google-console/
Endpoint expects a POST request with the following JSON payload:
{
"domains": ["example.com", "anotherdomain.com"],
"account": "<your_email@example.com>"
}The request must include an Authorization header with a Bearer token for authentication.
📬 Notifications
If a blocked domain is detected, the script sends a notification to the Telegram bot with the following messages - see Telegram Messages for more details.
🔁 Automatic Switching
The Link Switcher system will automatically switch any links associated with the blocked domains to their backup links via celery task - block_links_from_mail_tracker_domains_task, see Block Links by Mail Tracker Task. Notifications about the switch will be sent via the Telegram bot.