Receive flagged messages from GMail using IMAP4 and python

I found a lot of fictitious information about working with IMAP, but I did not understand how to use it for my purposes. I found how I can get ALL messages from my mailbox and ALL ONEs, but how do I work with stars? Please give me Python code examples for receiving messages with messages from GMail via IMAP4, for checking that a message is marked or undefined, in order to insert on one line and split any one message.

+5
source share
1 answer

Gmail "Flagged" status is displayed directly on the IMAP keyword \Flagged. Thus, you can switch the star of the message by setting or disabling \Flaggedin the message:

IMAP4.store(num, '+FLAGS', '\\Flagged')

You can search for marked messages by searching FLAGGED(or for unmanaged messages through UNFLAGGED):

IMAP4.search(None, 'FLAGGED')

Gmail even gives you a virtual folder containing all the marked messages. If you SELECT "[Gmail]/Starred", you will get an idea of ​​all the marked messages in the mailbox:

IMAP4.select('[Gmail]/Starred')
+8
source

All Articles