Search code examples
pythonexchangewebservicesexchangelib

Python exchangelib - mark item as read


I am trying to accomplish one task in my mailbox using Python's exchangelib module - how to move a certain email to a folder if it contains specific subject and has 'unread' status.

while True:
    print("Checking inbox...")
    for msg in acc.inbox.filter(subject="Kontrol fra EVT...", is_read=False):
        if "SOS" in msg.text_body:
            pass
        else:
            msg.is_read = True
            print("Moving to EVT folder...")
            msg.move(archive)
            time.sleep(0.5)
    time.sleep(5)

Everything appears to be working except for msg.is_read = True part. The message remains unread, despite being successfully moved to the required folder.

I believe I am missing something simple here. I tried googling and using official module's documentation but came up empty in this regard. Could find only one person with the same question as mine: Mark email as read with exchangelib

Thank you!


Solution

  • Found the answer myself while digging through modules' files. Apparently you have to "save" the item after flagging it. In the end my code should look like this:

    while True:
        print("Checking inbox...")
        for msg in acc.inbox.filter(subject="Kontrol fra EVT...", is_read=False):
            if "SOS" in msg.text_body:
                pass
            else:
                msg.is_read = True
                msg.save()
                print("Moving to EVT folder...")
                msg.move(archive)
                time.sleep(0.5)
        time.sleep(5)