I found many dummy info about working with IMAP, but I didn't understand how to use it for my purposes. I found how I can get ALL messages from mailbox and ALL SEEN messages, but how should I work with stars? Please, give me examples of python code for getting starred messages from GMail through IMAP4, for checking if some message is starred or unstarred, for starring and unstarring some one message.
Get starred messages from GMail using IMAP4 and python
Gmail's "Starred" state maps directly onto the IMAP \Flagged
keyword. So you can toggle a message's star by setting or unsetting \Flagged
on the message:
IMAP4.store(num, '+FLAGS', '\\Flagged')
You can search for starred messages by searching for FLAGGED
(or for unstarred messages via UNFLAGGED
):
IMAP4.search(None, 'FLAGGED')
Gmail even gives you a virtual folder containing all starred messages. If you SELECT "[Gmail]/Starred"
, you'll get a view of all the starred messages in the mailbox:
IMAP4.select('[Gmail]/Starred')
Hi, can you provide similar example in Java too? –
Enphytotic
The select option doesn't work for any of my accounts (I have three), I get
[NONEXISTENT] Unknown Mailbox: [Gmail]/Starred (now in authenticated state) (Failure)
My account is setup for Swedish locale, and I can see that for Drafts I need to use the Swedish word Utkast
, but for Starred
the word is Stjärnmärkt
. If I try [Gmail]/Stjärnmärkt
with IMAP's UTF-7 encoding, I get [NONEXISTENT] Unknown Mailbox: [Gmail]/Stj&AOQ-rnm&AOQ-rkt (now in authenticated state) (Failure)
–
Attorn It appears that the frontend and backend teams don't agree on what to call things: the IMAP folder is called
[Gmail]/Stjärnmärkta
(additional 'a'). –
Attorn from imap_tools import MailBox, AND
with MailBox('imap.mail.com').login('[email protected]', 'pwd', 'INBOX') as mailbox:
# get list of subjects of flagged emails from INBOX folder
subjects = [msg.subject for msg in mailbox.fetch(A(flagged=True))]
# set flag on emails from INBOX that html contains <b> tag
flags = [imap_tools.MailMessageFlags.FLAGGED]
mailbox.flag([m.uid for m in mailbox.fetch() if '<b>' in m.html], flags, True)
# print flags for all emails from INBOX
for msg in mailbox.fetch(): print(msg.date, msg.flags)
My external lib: https://github.com/ikvk/imap_tools
© 2022 - 2024 — McMap. All rights reserved.