So I am successful in getting the date for the last email received from the contact in the code below.
Now I am trying to get the date for the last email sent to the contact. As a beginner to the coding/python, I am not able to get it right. I tried to change 'From' to 'To' but it wasn't giving me the right date.
Any idea how should I proceed with this? I have tried looking around and didn't find a solution.
import email
from imapclient import IMAPClient
from datetime import timedelta, date, datetime
HOST = 'imap.gmail.com'
USERNAME = 'username'
PASSWORD = 'password'
ssl = True
## Connect, login and select the INBOX
server = IMAPClient(HOST, use_uid=True, ssl=ssl)
server.login(USERNAME, PASSWORD)
select_info = server.select_folder('INBOX')
since_date = date(2016, 1, 1)
##Search Inbox
messages = server.search(['FROM', '[email protected]', 'Since', since_date])
response = server.fetch(messages, ['RFC822'])
last_msg_id = list(response.keys())[-1]
data = response[last_msg_id]
msg_string = data[b'RFC822']
msg = email.message_from_string(msg_string.decode())
print('ID %d: From: %s Date: %s' % (last_msg_id , msg['From'], msg['date']))