Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python, IMAP and GMail. Mark messages as SEEN

I have a python script that has to fetch unseen messages, process it, and mark as seen (or read)

I do this after login in:

    typ, data = self.server.imap_server.search(None, '(UNSEEN)')

    for num in data[0].split():
        print "Mensage " + str(num) + " mark"
        self.server.imap_server.store(num, '+FLAGS', '(SEEN)')

The first problem is that, the search returns ALL messages, and not only the UNSEEN. The second problem is that messages are not marked as SEEN.

Can anybody give me a hand with this?

Thanks!

like image 404
Ezequiel Avatar asked Feb 12 '10 13:02

Ezequiel


2 Answers

import imaplib
obj = imaplib.IMAP4_SSL('imap.gmail.com', '993')
obj.login('user', 'password')
obj.select('Inbox')   <--- it will select inbox
typ ,data = obj.search(None,'UnSeen')
obj.store(data[0].replace(' ',','),'+FLAGS','\Seen')
like image 134
Avadhesh Avatar answered Oct 17 '22 05:10

Avadhesh


I think the flag names need to start with a backslash, eg: \SEEN

like image 24
Devo Avatar answered Oct 17 '22 05:10

Devo