Python exchangelib how to get today's unread mail list?

Viewed 2015

I'm trying to get only today's unread mail list by using Python exchanglib library. My code is like this below:

from exchangelib import Account,Credentials
import time

def connect_mail(username, pwd, addr):
    credentials = Credentials(username=username, password=pwd)
    account = Account(addr, credentials=credentials, autodiscover=True)
    return account

def get_unread_mail(account):
    res = account.inbox.filter(is_read=False)
    return res

a = connect_mail(username, pwd, addr)   
unread_mail_list = get_unread_mail(a)
current_date = time.strftime("%Y%m%d", time.localtime()) 
curr_mail_list = []
for mail in unread_mail_list :
    if str(mail.datetime_received)[:10].replace('-','') == current_date:
        curr_mail_list .append(mail)

This code may work , but actually this code will run for a long time cause i've a plenty of unread mails in my inbox.

And then I wanna solve it by using .filter().

Code is like this:

from exchangelib import EWSDate, EWSDateTime

def today_mail(account):
    today = EWSDate.today()
    mails = account.inbox.all().filter(start__gte=today)
    return mails

Or:

def today_mail(account):
    start = account.default_timezone.localize(EWSDateTime(2020, 6, 29))
    end = account.default_timezone.localize(EWSDateTime(2020, 7, 1))
    mails = account.inbox.all().filter(start__range=(start,end))

But unfortunately, it doesn't work.

Can anyone give me a hand on this? Thanks!

1 Answers
Related