Created
September 28, 2015 20:27
-
-
Save daverigby/bad685528940623702bc to your computer and use it in GitHub Desktop.
fix_date.py - corrects the received date in IMAP folders
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/python | |
# fix_date.py - corrects the received date in IMAP folders. | |
# Description: | |
# When moving mail between IMAP servers, the received date can become | |
# reset to the date when the message was added to the new server. This | |
# script attempts to fix this by setting the received date to the date the | |
# message was sent. | |
# Usage: | |
# Set the 5 variables below to suit your setup, and then run the script. | |
# If you run the script with the argument '--list', then it will just list | |
# all the mailboxes on the server. Useful if you don't know the exact name | |
# of the mailboxes. | |
# Notes: | |
# Due to the may IMAP works, you cannot change the recieved date for an | |
# existing message. To get round this, fix_date.py makes a new copy of the | |
# message in a different folder. As it has to copy each message, process | |
# can take some time! | |
# With some additional work the date could be set to the last received hop | |
# in the mail's delivery, but I didn't need that accuracy. | |
import imaplib | |
import email.Utils | |
import sys | |
# Change the following 5 lines as applicable: | |
source = "INBOX.Source" # The source folder to read the messages to fix. | |
dest = "INBOX.Dest" # The destination folder to save to new copy. | |
server = "imap.example.com" # IMAP server address | |
user = "user" # Username | |
passwd = "password" # Password | |
M = imaplib.IMAP4(server) | |
M.login(user, passwd) | |
if len(sys.argv) == 2 and sys.argv[1] == '--list': | |
print "List of mailboxes available on server:" | |
for mbox in M.list()[1]: | |
print mbox.split('"."')[1] | |
else: | |
M.select(source) | |
typ, data = M.search(None, 'ALL') | |
for num in data[0].split(): | |
typ, date = M.fetch(num, '(BODY[HEADER.FIELDS (DATE)])') | |
typ, body = M.fetch(num, '(RFC822)') | |
date_str = date[0][1][6:].strip() | |
datetime = email.Utils.parsedate(date_str) | |
print "Message (" + num + "), date:" + date_str | |
M.append(dest, None, imaplib.Time2Internaldate(datetime), body[0][1]) | |
#print body[0][1] | |
M.logout() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment