This Google Apps Script scans your Gmail inbox for a pre-define list of spam keywords and then deletes the mail it finds. It then sends you a summary of what was deleted together with an unsubscribe link if you want to unsubscribe from future spam.
- Keyword-based spam detection: Detects spam emails based on a customizable list of keywords.
- Unsubscribe link: Captures an Unsubscribe link for the user to use later.
- Detailed Summary: Sends you a summary of deleted emails, with links to find and restore them from trash if you want to.
- Go to Google Apps Script.
- Click the
New Project
button to create a new Apps Script project.
- Copy the script from this repository.
- In the Google Apps Script editor, paste the script into the main code editor window.
- Find and edit the gmail emails in the script - there are two places.
- Once the script is pasted, click the
File
menu and thenSave
. - Give the project a name like
Gmail Spam Cleaner
.
- Before running the script, Google Apps Script will request permissions to access your Gmail.
- Click
Run
->cleanUpSpam()
for the first time, and the script will prompt you to grant the necessary permissions. - Follow the prompts and approve the required permissions to allow the script to manage your Gmail inbox.
To make the script run automatically at a set interval (daily, weekly, etc.), you'll need to set up a trigger:
-
Open the Triggers Menu:
- In the Apps Script editor, click the clock icon in the left-hand toolbar (or go to
Triggers
->Add Trigger
from the top menu).
- In the Apps Script editor, click the clock icon in the left-hand toolbar (or go to
-
Create a Trigger:
- Choose which function to run: Select
cleanUpSpam
. - Select event source: Choose
Time-driven
. - Select type of time-based trigger: Select
Day timer
for daily cleanup, orWeek timer
for weekly cleanup. - Set time of day: Choose a specific time for the script to run (e.g., every morning).
- Choose which function to run: Select
-
Save the Trigger:
- Click
Save
, and the trigger will be scheduled. The script will now run automatically at the selected intervals to clean your inbox.
- Click
The list of keywords used to detect spam. The script searches for these keywords in both the subject line and body of the email. You can easily expand this list to add new keywords based on your preferences.
var spamKeywords = [
'unsubscribe', 'promotion', // Add more keywords as needed...
];
This list contains trusted email addresses or domains that you want to exclude from being marked as spam. These emails will not be affected by the script even if they match a keyword.
var allowList = [
'@myschool.edu',
'[email protected]',
'[email protected]'
];
Add your trusted contacts or domains here. You can expand this array with any other email addresses or domains you want to exclude from spam filtering.
Make sure to set your email address in the recipient variable to receive the summary report. The script sends a summary of deleted and unsubscribed emails to this address.
var recipient = "[email protected]";
Update this to your email address to ensure the summary of deleted and unsubscribed emails is delivered to you.
The script sends a detailed summary of:
Deleted Emails: Includes the email address, subject, and a link to recover each deleted email from the trash as well as an unsubscribe link.
Gmail Access: The script requires access to your Gmail account in order to read, filter, and delete emails, as well as to unsubscribe from certain senders.
If you have any suggestions for improvements or new features, feel free to open an issue or submit a pull request. Contributions are always welcome!