the following Python script should import eml files which are located within a specific folder and assign a pre-defined label. In general the script is working and doing its job.

There is just one small problem, instead of the timestamp form the eml, in the Gmail interface I see the timestamp from the import. This way emails are not blended into the timeline correctly.

Is it possible to adjust the "Received:" date to the date from the eml as it was originally?

I thought I tackled this already by setting the "internalDate".

import os
import base64
import email
import json
import imaplib
from google.oauth2.credentials import Credentials
from google_auth_oauthlib.flow import InstalledAppFlow
from googleapiclient.discovery import build
from datetime import datetime

# Gmail API settings
SCOPES = ['https://www.googleapis.com/auth/gmail.modify']
CLIENT_SECRET_FILE = 'credentials.json'
GMAIL_USER = '[email protected]'

# Label to use in Gmail (e.g., "backup")
GMAIL_LABEL = 'backup'

# Directory containing your local .eml files
LOCAL_EML_DIR = './eml/'

# Define a function to get a Gmail API service
def get_gmail_service():
    creds = None
    # The file token.json stores the user's access and refresh tokens.
    if os.path.exists('token.json'):
        creds = Credentials.from_authorized_user_file('token.json', SCOPES)
    if not creds or not creds.valid:
        flow = InstalledAppFlow.from_client_secrets_file(CLIENT_SECRET_FILE, SCOPES)
        creds = flow.run_local_server(port=0)
        # Save the credentials for the next run
        with open('token.json', 'w') as token:
            token.write(creds.to_json())
    service = build('gmail', 'v1', credentials=creds)
    return service

def get_label_id(service, label_name):
    results = service.users().labels().list(userId=GMAIL_USER).execute()
    labels = results.get('labels', [])
    
    for label in labels:
        if label['name'] == label_name:
            return label['id']
    
    return None

# Upload local .eml files to Gmail using the Gmail API
def upload_eml_files_to_gmail(service, label_name, eml_dir):
    label_id = get_label_id(service, label_name)  # Get the label ID by name
    if not label_id:
        print(f"Label '{label_name}' not found in Gmail.")
        return

    for filename in os.listdir(eml_dir):
        if filename.endswith('.eml'):
            eml_path = os.path.join(eml_dir, filename)
            
            # Read the .eml file
            with open(eml_path, 'rb') as eml_file:
                eml_data = eml_file.read()
            
            # Extract the INTERNALDATE from the EML file
            eml_message = email.message_from_bytes(eml_data)
            internaldate = eml_message.get('Date')
            
            # Convert internaldate to Unix timestamp and then to an integer
            internaldate = datetime.strptime(internaldate, '%a, %d %b %Y %H:%M:%S %z').timestamp()
            internaldate = int(internaldate)
            
            # Create a message with the eml file content and internalDate
            message = base64.urlsafe_b64encode(eml_data).decode('utf-8')
            
            message = {
                'raw': message,
                'internalDate': str(internaldate),
                'labelIds': [label_id]  # Assign the label to the message
            }
            
            # Upload the message to Gmail with the specified label
            service.users().messages().insert(userId=GMAIL_USER, body=message).execute()

# Main function to run the script
def main():
    service = get_gmail_service()  # Authenticate with Gmail API
    upload_eml_files_to_gmail(service, GMAIL_LABEL, LOCAL_EML_DIR)  # Upload .eml files to Gmail

if __name__ == '__main__':
    main()

Original time stamp: enter image description here

Gmail time stamp (shows the upload time): enter image description here

Message header in Gmail (shows both time stamps): enter image description here

0

There are 0 best solutions below