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 "A开发者_如何学Pythonll 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!
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/
in imap you don't have labels, gmail 'emulates' them on imap, you can low at the raw source of a message picked from imap an check if it has some custom header with the label
精彩评论