Hello i have issues parsing a date from an email with Python 2.6 Here's the code :
typ_sent, data_sent = sentbox.fetch(num_sent, '(RFC822)')
mail_sent = email.message_from_string(data_sent[0][1])
subject_sent = unicode(email.header.decode_header(mail_sent['Subject'])[0])
from_sent = unicode(email.header.decode_header(mail_sent['From'])[0])
datetime_sent = email.utils.mktime_tz(email.utils.parsedate_tz((mail_sent["Date"])))
datetime_sent = datetime.datetime.fromtimestamp(datetime_sent)
I have an error when trying to have the date time from the mail in the datetime object. Here's the message error.
File "checkIMAP_client.py", line 116, in <module>
datetime_sent = email.utils.mktime_tz(email.utils.parsedate_tz((mail_sent["Date"])))
File "/usr/lib64/python2.6/email/_parseaddr.py", line 142, in mktime_tz
if data[9] is None:
TypeError: 'NoneType' object is unsubscriptable
And i'll need to parse a datetime object to a valid email date format.
EDIT : In my script i need to compare emails from the sent items and inbox folders. I can read the date from the inbox emails (i use the append command on an email.message() object) But i can't read the datetime from the emails sent from my SMTP using smtplib.
I modified the code a bit to see what happens : Code :
datetime_sent = unicode(mail_sent["Date"])
print("Datetime sent =" + datetime_sent)
Prompt :
Datetime sent =None
I will look on how the Date is supposed to be implemented in the sent emails. It seems i have an issue here since the inbox mails haven't the issue.
EDIT : Ok, i found the issue. I'm sending emails from my server to a distant server with smtplib. When the distant server recieves the mail from my server on a certain adress, it redirects it back to original adress. My scripts can't read the datetime from the sent email. So what is the right format i need to use for email.parsedate_tz() to work ?
Ok, So i checked my parsing functions. It works when i send a mail from Thunderbird using my smtp server. I can get my datetime object. Stuff hit the fan when i'm sending my mail with python.