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?
You may use custom flag, example:
with MailBox('imap.mail.com').login('test@mail.com', 'pwd', 'INBOX') as mailbox:
# FLAG unseen messages in current folder as \Seen, \Flagged and TAG1
flags = (imap_tools.MailMessageFlags.SEEN, imap_tools.MailMessageFlags.FLAGGED, 'TAG1')
mailbox.flag(mailbox.uids(AND(seen=False)), flags, True)
https://github.com/ikvk/imap_tools/issues/123
Adding gmail label not implemented (not planned). Only search by them:
A(gmail_label='hello')