We can fetch a Gmail mailbox using Mailbox/IMAP_mailbox (imap-tools
) as follows:
from imap_tools import MailBox as IMAP_mailbox
# Init
mailbox = IMAP_mailbox(HOST)
mailbox.login(username=USERNAME, password=PASSWORD)
# Fetch
messages = mailbox.fetch(AND(seen=SEEN), bulk=BULK, mark_seen=MARK_SEEN, limit=LIMIT)
Then we can iterate through the messages as follows:
for message in messages:
print(message)
To mark the message as seen we can do the following:
mailbox.seen(message.uid, True)
However, instead of labelling it as seen, I would like assign a Gmail label to it. How can this be achieved? in this setup?