Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 667
Nathan Allan Avatar asked Jun 12 '26 13:06

Nathan Allan


1 Answers

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'])
like image 58
thangnd Avatar answered Jun 15 '26 01:06

thangnd



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!