所以我成功地从下面的代码中的联系人那里收到了最后一封电子邮件的日期。
现在我正在尝试获取发送给联系人的最后一封电子邮件的日期。作为编码/python 的初学者,我无法正确理解它。我试图将“从”更改为“到”,但它没有给我正确的日期。
知道我应该如何进行吗?我试过环顾四周,但没有找到解决方案。
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_i_want_to_search@gmail.com', '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']))