Skip to content
Advertisement

Marking an email as read python

I’d like to mark an email as read from my python code. I’m using

from exchangelib import Credentials, Account
my_account = Account(...)
credentials = Credentials(...)

to get access to the account. This part works great. I then get into my desired folder using this

var1 = my_account.root / 'branch1' / 'desiredFolder'

Again, this works great. This is where marking it as read seems to not work.

item = var1.filter(is_read=False).values('body')
for i, body in enumerate(item):
   #Code doing stuff
   var1.filter(is_read=False)[i].is_read = True
   var1.filter(is_read=False)[i].save(updated_fields=['is_read'])

I’ve tried the tips and answers from this post Mark email as read with exchangelib, but the emails still show as unread. What am I doing wrong?

Advertisement

Answer

I think you the last line of code that you save() do not work as you think that after you set is_read of unread[i] element to True, this unread[i] of course not appear in var1.filter(is_read=False)[i] again, so you acttually did not save this.
I think this will work.

for msg in my_account.inbox.filter(is_read=False):
    msg.is_read = True
    msg.save(updated_fields=['is_read'])
User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement