Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get starred messages from GMail using IMAP4 and python

Tags:

python

imap

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.

like image 684
Legeya Avatar asked Feb 24 '11 17:02

Legeya


1 Answers

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')
like image 193
dkarp Avatar answered Sep 22 '22 08:09

dkarp