简体   繁体   中英

Trying to return emails within past 24 hours using ExchangeLib

I'm trying to use ExchangeLib to return all emails within an inbox within the past 24 hours. I currently have it set up to return the most recent email in the inbox, I just need help with the 24 hours part. Here is what I have so far:

credentials = Credentials('My@email', 'password')
account = Account('My@email', credentials=credentials, autodiscover=True)

for item in account.inbox.all().order_by('-datetime_received')[:1]:
    print(item.subject, item.sender.email_address)

    html = item.unique_body

    soup = BeautifulSoup(html, "html.parser")
    for span in soup.find_all('font'):
        return(item.subject, item.sender.email_address, span.text)

I've been trying to look up references on how to go about this, but I'm honestly not finding much. Any recommendations?

You need to add a greater-than filter on the datetime_received field:

from datetime import timedelta
from exchangelib import UTC_NOW

since = UTC_NOW() - timedelta(hours=24)
for item in account.inbox.all()\
        .filter(datetime_received__gt=since)\
        .order_by('-datetime_received'):
    # Do something

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM