Skip to main content
 首页 » 编程设计

python之尝试使用 ExchangeLib 返回过去 24 小时内的电子邮件

2024年11月24日2zdz8207

我正在尝试使用 ExchangeLib 返回过去 24 小时内收件箱中的所有电子邮件。我目前已将其设置为返回收件箱中的最新电子邮件,我只需要 24 小时部分的帮助。这是我目前所拥有的:

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) 

我一直在尝试查找有关如何处理此问题的引用资料,但老实说,我没有找到太多。有什么建议吗?

请您参考如下方法:

您需要在 datetime_received 字段上添加大于过滤器:

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