This repository contains a Google Apps Script designed to filter and delete spam emails based on the presence of emojis in the sender's name or subject. The script can scan emails in the spam folder and either move them to trash or permanently delete them, depending on the function you choose to run.
- Enhancement: Improved emoji detection in email processing script.
- Explanation: Updated the
containsEmoji
function to detect Quoted-Printable and Base64 encoded emojis, as well as visible emojis.
- Explanation: Updated the
- Bug Fix: Resolved issues with detecting encoded emojis in sender names.
- Details: Adjusted regex patterns for better detection.
- New Feature: Added
logRawSubjectsInSpam
function.- Purpose: Logs raw subject lines and sender names of all emails in the spam folder for debugging purposes.
- Configuration: Added
enableLogging
variable to control logging behavior.- Details: Logs now include both sender name and subject for processed emails.
- Feature: Added logging functionality to Google Drive spreadsheet.
- Explanation: The script can now log email processing actions (checking, moving to trash, preparing to delete) to a specified Google Sheet for better tracking and debugging.
- Instructions:
- Create a New Spreadsheet: Create a new Google Spreadsheet and note the spreadsheet ID.
- Set Variables: In the script, set
logSpreadsheetId
to your spreadsheet ID andlogSheetName
to the desired sheet name. - Use
logMessage
Function: The script will automatically log messages to the specified sheet.
-
Open Google Apps Script:
- Go to Google Apps Script.
- Create a new project.
-
Copy the Script:
- Copy the content from the
Code.gs
file in this repository and paste it into the Google Apps Script editor.
- Copy the content from the
-
Save the Project:
- Save the project with a descriptive name.
- Enable Advanced Services:
- In the Apps Script Editor, go to "Services" in the sidebar.
- Enable the "Gmail API".
- Set the Detection Scope:
- At the beginning of the script, find the line
var searchScope = "either";
. - Modify the value of
searchScope
based on where you want the script to look for emojis:"sender"
: Only checks the sender's name for emojis."subject"
: Only checks the email subject for emojis."both"
: Checks both the sender's name and email subject for emojis."either"
: Checks either the sender's name or email subject for emojis.
- At the beginning of the script, find the line
- Run the Test Function:
- Execute the
testExtractAndLogProcessedEmails
function to log the sender names and subjects containing emojis. - Check the logs to ensure the correct emails are identified.
- Execute the
-
Create a Trigger:
- In the Apps Script editor, click on the clock icon in the left sidebar (Triggers).
- Click on "Add Trigger" at the bottom right.
-
Configure the Trigger:
- Choose which function to run:
processSpamEmails
. - Choose which deployment should run: Head.
- Select event source: Time-driven.
- Select type of time-based trigger: Choose the frequency that suits your needs (e.g., daily, hourly).
- Select time of day (if applicable): Choose a time that works best for you.
- Choose which function to run:
-
Save the Trigger:
- Click "Save" to create the trigger.
-
Create a Trigger:
- In the Apps Script editor, click on the clock icon in the left sidebar (Triggers).
- Click on "Add Trigger" at the bottom right.
-
Configure the Trigger:
- Choose which function to run:
permanentlyDeleteProcessedEmails
. - Choose which deployment should run: Head.
- Select event source: Time-driven.
- Select type of time-based trigger: Choose the frequency that suits your needs (e.g., daily, hourly).
- Select time of day (if applicable): Choose a time that works best for you.
- Choose which function to run:
-
Save the Trigger:
- Click "Save" to create the trigger.
processSpamEmails
: Moves the matching emails to trash based on the specified scope.permanentlyDeleteProcessedEmails
: Moves the matching emails to trash and then permanently deletes them by targeting their specific IDs using the Gmail API.deleteEmailsInTrash
: Uses the Gmail API to permanently delete specific emails from the trash.extractSenderName
: Extracts the sender name from raw email content.containsEncodedEmoji
: Checks if the sender name contains any encoded emoji sequences.containsEmoji
: Checks if the text contains any visible emojis.testExtractAndLogProcessedEmails
: Logs sender names and subjects for verification.