This tool allows you to transfer emails from one Gmail account to another, preserving labels and handling rate limits. It"s particularly useful for archiving emails from user accounts that are being decommissioned.
- Node.js (v14 or later)
- npm (comes with Node.js)
- A Google Cloud Project
- Two Gmail accounts: a source account (to transfer from) and an archive account (to transfer to)
- Go to the Google Cloud Console.
- Create a new project or select an existing one.
- Enable the Gmail API:
- In the sidebar, navigate to "APIs & Services" > "Library".
- Search for "Gmail API" and click on it.
- Click "Enable".
- In the Google Cloud Console, go to "IAM & Admin" > "Service Accounts".
- Click "Create Service Account".
- Enter a name for the service account (e.g., "gmail-archiver").
- Click "Create and Continue".
- For "Select a role", choose "Basic" > "Editor".
- Click "Continue" and then "Done".
- In the Service Accounts list, find the account you just created.
- Click on the three dots in the "Actions" column and select "Manage keys".
- Click "Add Key" > "Create new key".
- Choose "JSON" as the key type and click "Create".
- The key file will be downloaded to your computer. Keep this file secure and do not share it.
- On the Service Accounts page, click on your service account.
- Under "Domain-wide delegation", click "View Client ID".
- Copy the Client ID (you"ll need this later).
- Go to your Google Workspace Admin Console.
- Navigate to Security > Access and data control > API Controls.
- In the "Domain-wide Delegation" section, click "Manage Domain Wide Delegation".
- Click "Add new".
- In the "Client ID" field, paste the Client ID you copied earlier.
- In the "OAuth Scopes" field, enter:
https://www.googleapis.com/auth/gmail.modify
- Click "Authorize".
-
Clone this repository:
git clone https://github.com/your-repo/gmail-email-archiver.git cd gmail-email-archiver
-
Install dependencies:
npm install
-
Create a
.env
file in the project root with the following content:GOOGLE_APPLICATION_CREDENTIALS=/path/to/your/service-account-key.json [email protected]
Replace
/path/to/your/service-account-key.json
with the actual path to the JSON key file you downloaded earlier, and[email protected]
with the email address of your archive account.
To run the script, use the following command:
npm run start -- [email protected]
Replace [email protected]
with the email address of the account you want to transfer emails from.
To perform a dry run (which doesn"t actually transfer any emails):
npm run start -- --dry-run [email protected]
or
npm run start -- -d [email protected]
- The script uses rate limiting to avoid hitting Google"s API quotas. If you encounter rate limit errors, you may need to adjust the rate limit in
utils/rateLimiter.ts
. - The script transfers emails in batches of 100. For very large mailboxes, the process may take a long time.
- Make sure you have sufficient storage in the archive account to accommodate all the emails being transferred.
- It"s recommended to run a dry run first to see what would be transferred without actually moving any emails.
- This script does not delete emails from the source account. If you need to delete emails after transfer, you"ll need to modify the script or perform this action manually.
- If you encounter authentication errors, double-check that your service account key is correct and that you"ve properly set up domain-wide delegation.
- If you hit rate limits, try reducing the rate in
utils/rateLimiter.ts
(e.g., changenew RateLimiter(2)
tonew RateLimiter(1)
). - For any other errors, check the console output for error messages. The script includes error logging that should help identify the issue.
Contributions are welcome! Please feel free to submit a Pull Request.
This project is licensed under the MIT License - see the LICENSE file for details.