This is a smaller portion of a bigger project. I need to only get unread emails and a parse their headers. How can I modify the following script to only get unread emails?>
You may use imap_tools package:
https://pypi.org/project/imap-tools/
from imap_tools import MailBox, AND
with MailBox('imap.mail.com').login('test@mail.com', 'password', 'INBOX') as mailbox:
# get unseen emails from INBOX folder
for msg in mailbox.fetch(AND(seen=False)):
print(msg.headers)