Originally created for Northside Independent School District (NISD). Generalized and released for public use.
A Google Apps Script that forwards all Gmail threads (with attachments) to a personal email address. It tracks progress using a Gmail label to avoid duplicates, handles Apps Script execution time limits gracefully, and supports a dry-run mode for safe testing.
- Searches for all threads without the "Forwarded" label.
- Forwards every message in each thread to your configured email.
- Applies the "Forwarded" label so threads aren't processed again.
- Stops safely before hitting the 6-minute Apps Script time limit and can be re-run to continue.
git clone https://github.com/YOUR_USERNAME/gmail-forwarding-script.git
cd gmail-forwarding-scriptnpm installInstall the Clasp CLI and authenticate:
npm install -g @google/clasp
clasp loginCreate a .clasp.json file in the project root (this file is git-ignored):
{
"scriptId": "YOUR_SCRIPT_ID_HERE",
"rootDir": "./src"
}You can find your scriptId in the Apps Script editor under Project Settings.
In the Apps Script editor, go to Project Settings → Script Properties and add:
| Key | Value |
|---|---|
PERSONAL_EMAIL |
The email address to forward messages to |
clasp push- Open your project at script.google.com.
- Select
forwardEmailsfrom the function dropdown. - Click Run.
- Authorize the Gmail permissions when prompted.
The script starts in dry-run mode (DRY_RUN = true in src/Code.js). It logs which threads would be forwarded without sending anything. Check the execution log to verify it's picking up the right emails.
- Set
DRY_RUN = falseinsrc/Code.js. - Push again:
clasp push - Run
forwardEmailsfrom the Apps Script editor.
The script processes threads in batches of 100. If it approaches the time limit, it stops and logs a message — just run it again to continue.
| Key | Description |
|---|---|
PERSONAL_EMAIL |
The destination email address for forwarded messages |
To forward new emails on a schedule:
- In the Apps Script editor, go to Triggers (clock icon).
- Click Add Trigger.
- Set function to
forwardEmails, event source to Time-driven, and choose an interval (e.g., every 5 minutes).
gmail-forwarding-script/
├── src/
│ ├── Code.js # Main script with all functions
│ └── appsscript.json # Manifest with OAuth scopes and runtime config
├── tests/
│ └── Code.test.js # Property-based tests for email validation
├── .clasp.json # Clasp config (git-ignored, contains scriptId)
├── .claspignore # Controls which files get pushed to Apps Script
├── .gitignore # Version control exclusions
├── LICENSE # MIT License
├── package.json # Node.js dev dependencies (Jest, fast-check)
└── README.md # This file
Defined in src/appsscript.json:
| Scope | Purpose |
|---|---|
https://mail.google.com/ |
Full Gmail access — read, send, and manage emails and labels |
npm testTests use Jest and fast-check for property-based testing of the email validation logic.
MIT — Copyright (c) 2025 Alvaro Gomez