0

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.

Tommyka
  • 665
  • 1
  • 5
  • 9
  • 1
    Have you looked at [OfflineIMAP](http://offlineimap.org/) at all? It supports moving IMAP email between sources (including local files, and GMail), preserving dates and status flags as needed. Implemented in Python. – Martijn Pieters Oct 22 '12 at 09:34
  • I didnt know of that. I was hoping to get my current code working, instead of looking at a new solution. It is only the date I need to get working, for my own solution to be working. – Tommyka Oct 22 '12 at 11:22

1 Answers1

0

Try escaping the quotes. date = '\"' + headerdic["Date"] + '\"'

kkurian
  • 3,844
  • 3
  • 30
  • 49