簡體   English   中英

python exchangelib 如何在特定日期范圍內獲取電子郵件?

[英]python exchangelib how to get email within specific date range?

我嘗試使用exchangelib提取組郵箱進行分析,我想在一個日期范圍內提取。
嘗試使用過濾器功能,但似乎只適用於日歷,請問您是否有電子郵件示例? 謝謝大家。

您需要過濾消息項目上可用的日期時間字段。 Message.FIELDS包含Message類上的所有可用字段。 您可以使用以下內容列出所有日期時間字段:

>>> [f.name for f in Message.FIELDS if f.value_cls == EWSDateTime]
['datetime_received', 'datetime_sent', 'datetime_created', 'reminder_due_by', 'last_modified_time']

自述文件顯示了使用.filter(start__range(x, y))示例,但start字段僅適用於CalendarItem對象。 相反,使用例如datetime_received來過濾Message對象:

tz = EWSTimeZone.localzone()
emails_from_2017 = account.inbox.filter(datetime_received__range=(
    tz.localize(EWSDateTime(2017, 1, 1)),
    tz.localize(EWSDateTime(2018, 1, 1))
))
pytz_tz = pytz.timezone('Europe/Copenhagen')
py_dt = pytz_tz.localize(datetime(year,month,day))
ews_bfr = EWSDateTime.from_datetime(py_dt)

for item in account.inbox.all().order_by('-datetime_received')[:10000]:
    if item.datetime_received < ews_bfr:
        item.delete() #delete all filtered emails
        print("Mail deleted Successfully")

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM