I am working on a Python tool to migrate emails into Gmail while preserving the original Date header. My goal is simply to build a cli tool that allows to copy email from gmail account a to gmail account b, preserving all data and metadata (including date and time).
I am using the Gmail API's users.messages.insert method, as suggested in the Google support documentation. The support states that using internalDateSource: 'dateHeader' and deleted: true should enforce the Date header from the email: https://support.google.com/vault/answer/9987957?hl=en
Here is a minimal code example:
from googleapiclient.discovery import build
import base64
# Initialize Gmail API client
service = build('gmail', 'v1', credentials=your_credentials)
# Raw email with a custom Date header
raw_email = """\
Content-Type: text/plain; charset="us-ascii"
MIME-Version: 1.0
Content-Transfer-Encoding: 7bit
to: [email protected]
from: [email protected]
subject: Test Email
Date: Tue, 13 Aug 2024 14:00:00 +0000
This is a test email.
"""
# Encode the email
raw_email_b64 = base64.urlsafe_b64encode(raw_email.encode('utf-8')).decode('utf-8')
# Insert the email using the Gmail API
body = {
'raw': raw_email_b64,
'internalDateSource': 'dateHeader',
'deleted': True
}
response = service.users().messages().insert(userId='me', body=body).execute()
# Log the response
print(response)
Problem: Despite setting internalDateSource: 'dateHeader' and deleted: true, the Date header in the inserted email is overridden by the current timestamp. The original Date header is not preserved and the datetime of insert is used instead.
Question: Is this behavior expected, or am I missing something in the implementation? Are there additional steps required to enforce the Date header during email insertion? Any insights or workarounds would be greatly appreciated!
Verified that the Date header is correctly set in the raw email before insertion. Used the internalDateSource: 'dateHeader' parameter as per the Google support suggestion. Added the deleted: true parameter to the users.messages.insert method. Observations: The Gmail API still overrides the Date header with the current timestamp. The X-Original-Date header workaround works, but I would prefer to rely on the Date header directly.


dateHeaderasEnumvalue - so maybe normally it has assigned integer value and you would have to use this integer value instead of string"dateHeader"