Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python IMAP: Cannot mark email as seen

I'm trying to set an email as read using the Python IMAP library. Just like in this example: Python, IMAP and GMail. Mark messages as SEEN

I select a GMail account's "All email" folder then obtain the UID of a specific email through the search command. Then I use that UID to try and mark it as read, but fail.

    result, data = mail.uid('fetch', email_uid, '(FLAGS)')

    print "RESULT: ",result,data

    mail.select(folder_name, readonly=False)
    result, data = mail.store(email_uid, '+FLAGS', '\\Seen')

    print "RESULT OF OPERATION FOR UID",email_uid,result,data

No matter how many times I run this piece of code I always obtain the flags not containing the \Seen flag.

Sample execution (ignore Django prints):

FINAL QUERY: (SENTSINCE 07-Mar-2013 SENTBEFORE 11-Mar-2013 LARGER 7508 SMALLER 7510)
RESULT:  OK ['31424 (UID 54264 FLAGS (NotJunk $NotJunk))']
RESULT OF OPERATION FOR UID 54264 OK [None]

[09/Mar/2013 17:30:37] "GET /api/mark_as_read/320/?token=8e3c057b841b75c864685786b2a9657aadf17e3bfb991b103b7c4b3ffdd2a753&refresh_mode=all&folder_mode=inbox HTTP/1.1" 200 144
FINAL QUERY: (SENTSINCE 07-Mar-2013 SENTBEFORE 11-Mar-2013 LARGER 7508 SMALLER 7510)
RESULT:  OK ['31424 (UID 54264 FLAGS (NotJunk $NotJunk))']
RESULT OF OPERATION FOR UID 54264 OK [None]

[09/Mar/2013 17:30:44] "GET /api/mark_as_read/320/?token=8e3c057b841b75c864685786b2a9657aadf17e3bfb991b103b7c4b3ffdd2a753&refresh_mode=all&folder_mode=inbox HTTP/1.1" 200 144
FINAL QUERY: (SENTSINCE 07-Mar-2013 SENTBEFORE 11-Mar-2013 LARGER 7508 SMALLER 7510)
RESULT:  OK ['31424 (UID 54264 FLAGS (NotJunk $NotJunk \\Seen))']
RESULT OF OPERATION FOR UID 54264 OK [None]

I tried multiple variations. I tried setting the flag \Seen, Seen, \Seen, SEEN, with parenthesis, without parenthesis, and have no idea what I might try apart from this.

I suspect there might be some very specific detail here - maybe the GMail "All email" folder does not allow to mark an email as read?

I also don't know if, in the (result,data) tuple returned in the mail.store command, if the data variable is supposed to have the "[None]" variable for a normal execution, or if it means that something is wrong.

If I try to set a wrong flag like \\Seen the mail.store command raises an error. So when setting the \Seen flag it is a valid IMAP flag.

I have no idea what to try next.

Any help would be very appreiated.


1 Answers

The IMAP specification for the STORE command does not have an option to set flags by UID. You can only set them (like in the example you linked to) by message set.
The only reason it works sometimes is that the sequences for your UID's and message set's are relatively close (31424 vs 54264).

RESULT:  OK ['31424 (UID 54264 FLAGS (NotJunk $NotJunk))']

You're not actually ever setting the flags by UID, you're setting them by message set...which occasionally corresponds to a UID.


You can set flags by UID however (just not with a STORE command). You can issue an UID Command with the first argument being store to set flags:

result, data = mail.uid('store', '542648', '+FLAGS', '(\\Seen)')  

If successful, the server will respond with the message set corresponding to the UID you used -eg (using your example):

OK ['31424 (FLAGS (\\Seen))']
like image 150
Gerrat Avatar answered Jul 02 '26 09:07

Gerrat



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!