Iam working on a script to move mails from a imap server to gmail via imap.
I got it to copy the mails over and create the labels, but it sets the date to be now instead of to original date, when i look in gmail.
# fetch header of current mail form old server to get date
result, header = From.uid('fetch', num, '(BODY[HEADER.FIELDS (DATE SUBJECT)])')
headerdic = headerparser.parsestr(header[0][1])
# add dobble qoutes around date
date = '"' + headerdic["Date"] + '"'
# get mail content
result, data = From.uid('fetch', num, '(RFC822)')
mgs = data[0][1]
# append mail to server
To.append(folder_name, None, date, mgs)
I read in the in the documentation that the dobble qoutes was importat, but it didnt seams to do a difference.
update
I found that the following solution worked
result, header = From.uid('fetch', num, '(BODY[HEADER.FIELDS (DATE SUBJECT)])')
headerdic = headerparser.parsestr(header[0][1])
pz = email.utils.parsedate_tz(headerdic["Date"])
stamp = email.utils.mktime_tz(pz)
date = imaplib.Time2Internaldate(stamp)
result, data = From.uid('fetch', num, '(RFC822)')
mgs = data[0][1]
To.append(folder_name, None, date, mgs)
Its possible that i had the right solution earlier, but my mails was still in the trash in gmail, so it just reused a earlier test where i did not send the date.