How to change email flag to recent using IMAPClient

I receive emails from my mail server using IMAPClient (Python), checking that email messages are marked as "\ Recent". After the message has been read, the mail server automatically sets the email flag to "\ Seen".

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

What I find is that IMAPClient throws an exception when I try to add the "\ Recent" flag to an email using the set_flag definition of IMAPClient. Adding any other flag works fine.

The IMAPClient documentation states that the Recent flag is read-only, but I was wondering if there was a way to mark the email as unreadable.

From my understanding of email software such as Thunderbird, you can set emails as unreadable, so I guess there should be a way to do this.

Thanks.

+4
source share
4 answers

Disclaimer: I am familiar with IMAP, but not Python-IMAPClient.

Typically, the 'seen' flag determines whether the email summary is displayed as normal or bold. You must have reset the visible flag. However, a recent flag may not be under your direct control. The imap server will install it if it notifies you of new messages.

+1
source

For completeness, here is an example of using IMAPClient. The \ Seen flag is updated 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 only flushes the flags specified. When setting read / unread status, you usually want to leave any other message flags intact.

It is also worth noting that it is possible to call fetch using the BODY.PEEK data element to retrieve parts of messages without affecting the \ Seen flag. This may eliminate the need to set the \ Seen checkbox after downloading the message.

See section 6.4.5 of RFC 3501 for details.

+3
source

IMAPClient docs indicate that 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: perhaps not a limitation of IMAPClient).

Use the \ Seen flag to mark something unread.

+1
source

@Menno Smits:

I am having problems adding the '\ Seen' flag to mail after parsing. I want to mark mail as READ when it contains certain text.

I am trying to use add_flags using "client.add_flags (msg_ids, [SEEN])", you specified above, but I continue to receive storage : the command was received in an invalid state . What exactly goes into [SEEN] (is it just placeholder or exact syntax?)

Here is part 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' 
0
source

Source: https://habr.com/ru/post/1393682/


All Articles