Question

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.

Was it helpful?

Solution

Disclaimer: I'm familiar with IMAP but not Python-IMAPClient specifically.

Normally the 'seen' flag determines if an email summary will be shown normal or bold. You should be able to reset the seen flag. However the recent flag may not be under your direct control. The imap server will set it if notices new messages arriving.

OTHER TIPS

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.

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.

@Menno Smits:

I'm having issues adding the '\Seen' flag to a mail after parsing through it. I only want to mark a mail as READ when it contains a particular text.

I've been trying to use the add_flags using the "client.add_flags(msg_ids, [SEEN])" you gave above but I keep getting store failed: Command received in invalid state What exactly goes into the [SEEN](is this just a placeholder or the exact syntax?)

Here is a portion of my code:

#login and authentication
context=ssl.SSLContext(ssl.PROTOCOL_TLSv1_2)
iobj=imapclient.IMAPClient('outlook.office365.com', ssl=True,ssl_context=context)
iobj.login(uname,pwd)
iobj.select_folder('INBOX', readonly=True)
unread=iobj.search('UNSEEN')
print('There are: ',len(unread),' UNREAD emails')
for i in unread:
  mail=iobj.fetch(i,['BODY[]'])
  mail_body=html2text.html2text(mcontent.html_part.get_payload().decode(mcontent.html_part.charset))
  ##Do some regex to parse the email to check if it contains text
  meter_no=(re.findall(r'\nACCOUNT NUMBER: (\d+)', mail_body))
  req_type=(re.findall(r'Complaint:..+?\n(.+)\n', mail_body))
  if 'Key Change' in req_type:
        if meter_no in kct['Account_no'].values:
            print 'Going to sendmail'# Call a function                                              
            sending_email(meter_no,subject,phone_no,req_type,)              
            mail[b'FLAGS']=r'b\Seen'+','+''+r'b\Answered'##Trying to manuaally alter the flag but didn't work##
            iobj.add_flags(i,br'\Seen')# Didn't work too  (but is 'i' my msg_id??)
            iobj.add_flags(i,[SEEN]) # Complains Name SEEN not defined

        else: print 'KCT is yet to be generated'
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top