Skip to content

About

Standalone Python utility that fixes incorrect "today" timestamps on imported emails in Gmail after IMAP migrations by extracting original RFC2822 Date headers and re-appending messages in-place.

Topics

Resources

Stars

0 stars

Watchers

0 watching

Forks

Latest commit

 

History

10 Commits

Folders and files

NameName
Last commit message
Last commit date
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Gmail IMAP Timestamp Fixer

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 historical INTERNALDATE metadata, and purges the incorrect duplicates from Gmail.


Buy Me A Coffee


🛑 The Problem

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.

🚀 The Solution

This tool automates the in-place repair process:

  1. Connects directly to your target Gmail / Google Workspace account over IMAP.
  2. Iterates through all folders (or a single folder) and parses each email's MIME body to extract its true original Date: header.
  3. Re-appends the email to Gmail using an explicitly formatted RFC3501 INTERNALDATE string.
  4. Safely removes the original email with the incorrect import timestamp.
  5. Preserves all message flags, attachments, formatting, and folder structures.

🔑 Prerequisites & Account Setup

Modern email providers require App-Specific Passwords for automated script access:

  1. Go to your Google Account Settings (2-Step Verification must be enabled).
  2. Direct link: Google App Passwords.
  3. Create a new app password (e.g. named "Timestamp Fixer").
  4. Copy the 16-digit code (without spaces or hyphens, e.g. abcd1234efgh5678).

⚙️ Usage & Execution

Set your Target Account environment variables before executing the script. Choose the command block corresponding to your operating system:

Option A: Linux / macOS (Terminal)

# 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

Option B: Windows CMD (Command Prompt)

:: 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

Option C: Windows PowerShell

# 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

🛡️ Reliability & Safety Features

  • 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-ID verification 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.

📜 License

Licensed under the MIT License. Free to use, adapt, and share.

About

Standalone Python utility that fixes incorrect "today" timestamps on imported emails in Gmail after IMAP migrations by extracting original RFC2822 Date headers and re-appending messages in-place.

Topics

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages