Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to change email flag to Recent using IMAPClient

Tags:

python

imaplib

I am retrieving emails from my email server using IMAPClient (Python), by checking for emails flagged with "\Recent". After the email has been read the email server automatically sets the email flag to "\Seen".

What I want to do is reset the email flag to "\Recent" so when I check the email directly on the server is still appears as unread.

What I'm finding is that IMAPClient is throwing an exception when I try to add the "\Recent" flag to an email using IMAPClient's "set_flag" definition. Adding any other flag works fine.

The IMAPClient documentation say's the Recent flag is read-only, but I was wondering if there is still a way to mark an email as un-read.

From my understanding email software like Thunderbird allows you to set emails as un-read so I assume there must be a way to do it.

Thanks.

like image 776
user788462 Avatar asked Feb 21 '23 16:02

user788462


2 Answers

For completeness, here's an actual example using IMAPClient. The \Seen flag is updated in order to control whether messages are marked as read or unread.

from imapclient import IMAPClient, SEEN

client = IMAPClient(...)
client.select_folder('INBOX')
msg_ids = client.search(...)

# Mark messages as read
client.add_flags(msg_ids, [SEEN])

# Mark messages as unread
client.remove_flags(msg_ids, [SEEN])

Note that add_flags and remove_flags are used instead of set_flags because the latter resets the flags to just those specified. When setting the read/unread status you typically want to leave any other message flags intact.

It's also worth noting that it's possible call fetch using the "BODY.PEEK" data item to retrieve parts of messages without affecting the \Seen flag. This can avoid the need to fix up the \Seen flag after downloading a message.

See section 6.4.5 of RFC 3501 for more details.

like image 179
Menno Smits Avatar answered Mar 06 '23 17:03

Menno Smits


IMAPClient docs specifically stated the '\Recent' flag is ReadOnly:

http://imapclient.readthedocs.org/en/latest/#message-flags

This is probably a feature (or limitation) of IMAP and IMAP servers. (That is: probably not an IMAPClient limitation).

Use the '\Seen' flag to mark something unread.

like image 22
Dan H Avatar answered Mar 06 '23 17:03

Dan H