Post-Migration Date Recovery Tool: Fixes incorrect email timestamps in Gmail after IMAP migrations. Automatically extracts the original
Date:header from MIME bodies, recreates the emails with correct historicalINTERNALDATEmetadata, and purges the incorrect duplicates from Gmail.
When migrating emails to Gmail via standard IMAP tools or third-party importers, network drops, server timeouts, or improper APPEND flags often cause Gmail to assign the current import timestamp (INTERNALDATE) to historical messages.
As a result, emails that are 10 or 20 years old all appear in Gmail's web interface as if they were received today at the exact time of the migration. Because IMAP's INTERNALDATE is strictly read-only after creation, standard clients cannot update these timestamps in-place.
This tool automates the in-place repair process:
- Connects directly to your target Gmail / Google Workspace account over IMAP.
- Iterates through all folders (or a single folder) and parses each email's MIME body to extract its true original
Date:header. - Re-appends the email to Gmail using an explicitly formatted RFC3501
INTERNALDATEstring. - Safely removes the original email with the incorrect import timestamp.
- Preserves all message flags, attachments, formatting, and folder structures.
Modern email providers require App-Specific Passwords for automated script access:
- Go to your Google Account Settings (2-Step Verification must be enabled).
- Direct link: Google App Passwords.
- Create a new app password (e.g. named "Timestamp Fixer").
- Copy the 16-digit code (without spaces or hyphens, e.g.
abcd1234efgh5678).
Set your Target Account environment variables before executing the script. Choose the command block corresponding to your operating system:
# Set your Gmail / Google Workspace credentials
export TGT_USER="your-email@gmail.com"
export TGT_PASS="abcd1234efgh5678"
# Run the fixer script
python3 gmail_timestamp_fixer.py:: Set your Gmail / Google Workspace credentials
set TGT_USER=your-email@gmail.com
set TGT_PASS=abcd1234efgh5678
:: Run the fixer script
python gmail_timestamp_fixer.py# Set your Gmail / Google Workspace credentials
$env:TGT_USER="your-email@gmail.com"
$env:TGT_PASS="abcd1234efgh5678"
# Run the fixer script
python gmail_timestamp_fixer.py- Auto-Reconnect & Throttling: Built-in SSL connection retry loops and throttling delays (
time.sleep(0.3)) prevent Google rate-limits and socket resets during large repair runs. - mUTF-7 Safe: Handles localized folder names with umlauts or non-ASCII characters (
ä,ö,ü,ß). - Duplicate Prevention: Uses
Message-IDverification or MD5 fallback hashing to prevent creating duplicate emails during repair interruptions. - System Folders Ignored: Standard system folders (
[Gmail],Trash,Spam, etc.) are automatically skipped to avoid corrupting special system structures.
Licensed under the MIT License. Free to use, adapt, and share.