因此,我是 Python 和编码的初学者,并试图构建一个代码,该代码将提供有关从联系人收到的最后一封电子邮件的信息。现在,我给出了从联系人收到的所有电子邮件的输出。我只想让它打印最后一封电子邮件。
关于如何去做的任何建议?
import email
from imapclient import IMAPClient
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')
messages = server.search(['FROM', 'email_of_the_contact@gmail.com'])
response = server.fetch(messages, ['RFC822'])
for msgid, data in response.items():
msg_string = data[b'RFC822']
msg = email.message_from_string(msg_string.decode())
print('ID %d: From: %s Date: %s' % (msgid, msg['From'], msg['date']))