Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python/imaplib - How to get messages' labels?

I'm using imaplib for my project because I need to access gmails accounts. Fact: With gmail's labels each message may be on an arbitrary number of folders/boxes/labels.

The problem is that I would like to get every single label from every single message.

The first solution it cames to my mind is to use "All Mail" folder to get all messages and then, for each message, check if that message is in each one of the available folders.

However, I find this solution heavy and I was wondering if there's a better way to do this.

Thanks!

like image 698
jpaires Avatar asked May 25 '11 10:05

jpaires


1 Answers

To get all the labels for a given message within gmail you can do the following

t, d = imapconn.uid('FETCH', uid, '(X-GM-LABELS)')
or
t, d = imapconn.fetch(uid, '(X-GM-LABELS)')

BTW: You can find more on gmail imap extensions at http://code.google.com/apis/gmail/imap/

like image 103
molicule Avatar answered Sep 21 '22 21:09

molicule