|
|
||
|---|---|---|
| .. | ||
| src | ||
| .clasp.json | ||
| AGENTS.md | ||
| README.md | ||
| package-lock.json | ||
| package.json | ||
README.md
Mail Forwarding Automation
This directory contains an Infrastructure-as-Code (IaC) deployment for Google Apps Script. It automates the creation and synchronization of Google Workspace mail forwarding by reading from a Google Sheet (typically populated by Google Forms) and managing Workspace Groups.
Prerequisites
To deploy this code to your Google Workspace, you need the following installed on your machine:
- Node.js & npm
- Google Clasp CLI: Run
npm install -g @google/clasp
Step 1: Authentication & Setup
Before you can deploy, you must authenticate your local machine with your Google Workspace Admin account and enable the Apps Script API.
- Enable the API: Go to https://script.google.com/home/usersettings and turn ON the "Google Apps Script API".
- Login: In your terminal, run:
This will open a browser window. Sign in with your Workspace Admin account (clasp login@haumdaucher.de) and grant the necessary permissions.
Step 2: Configuration
You must configure the script to point to your specific Google Sheet.
Open src/Code.js and modify the CONFIG block at the top of the file:
const CONFIG = {
// 1. The ID of the Google Sheet (found in the URL: https://docs.google.com/spreadsheets/d/<THIS_ID>/edit)
SPREADSHEET_ID: "YOUR_SHEET_ID_HERE",
// 2. The name of the tab at the bottom of the screen
SHEET_NAME: "Form Responses 1",
// 3. The column numbers containing the data (1 = A, 2 = B, 3 = C, etc.)
COL_FORWARD_ADDRESS: 6,
COL_FORWARD_TO_ADDRESS: 4,
// 4. Your admin email for receiving reports
ADMIN_EMAIL: "admin@haumdaucher.de",
// 5. Dry run mode. If true, script logs intended changes without modifying Workspace.
DRY_RUN: true,
// ... leave the GROUP_DESCRIPTION_TAG untouched!
};
Step 3: Deployment
Once configured, you need to create an Apps Script project in your Google Account and push this code to it.
- Navigate to this
mail_forwardingdirectory in your terminal. - Initialize the project as a standalone script:
(This creates a hiddenclasp create --type standalone --title "Haumdaucher Mail Forwarding" --rootDir ./src.clasp.jsonfile linking this directory to the cloud project). - Push the code:
clasp push
Step 4: Initialization
The code is now in the cloud, but the background triggers need to be activated and the Admin SDK authorized.
- Open the project in your browser:
clasp open-script - Ignore the large blue "Deploy" button. You do not need to create a deployment. This script runs via background triggers, not as a web app.
- In the toolbar directly above the code editor, look for a dropdown menu showing function names (it might currently say
syncForwardings). Click the dropdown and selectsetup. - Click the Run button (the play icon) right next to the dropdown.
- Authorization Required: Google will prompt you to review permissions. (Note: This interactive browser consent screen is a strict Google Workspace security requirement for scripts accessing the Admin API, which is why this specific step cannot be automated via CLI).
- Click "Review Permissions"
- Choose your Admin account.
- Click "Advanced" -> "Go to Haumdaucher Mail Forwarding (unsafe)"
- Click "Allow" to grant access to the Admin Directory API, Gmail API, and Google Sheets.
- Once authorized, the
setupfunction will finish executing. It installs the backgroundonChangetrigger.
System Status: LIVE
The system is currently configured for active production (DRY_RUN: false). Any changes to the Google Sheet will result in real-time creation, modification, or deletion of Google Workspace Groups.
To revert to dry-run mode for testing:
- Open
src/Code.jsand changeDRY_RUN: falsetoDRY_RUN: true. - Run
clasp push.
You are done! Whenever a new response is submitted to the configured Google Sheet via Forms, the script will automatically run in the background, reconcile the forwarding groups, and send you an email report.
Troubleshooting & Analysis
If you find that the script is not triggering when a new form is submitted:
- Check Executions: Run
clasp open-scriptand click on the Executions tab (the list/bullet icon) in the left sidebar.- No entries? The trigger never fired. You may need to run the
setup()function again to ensure the triggers are installed. - Failed entries? Click on the failed execution to see the exact error message and stack trace.
- No entries? The trigger never fired. You may need to run the
- Re-Run Setup: If you have pushed new code or changed your Google Account permissions, always re-run the
setupfunction in the Apps Script UI to refresh the background triggers and authorization. - Trigger Source: Ensure the Google Sheet you are using is the same one linked to your Google Form. The
onFormSubmittrigger only fires on the specific sheet receiving the form responses.