Python exchangelib:检查项目是否是消息
Python exchangelib: check if item is a message or not
我在使用 exchangelib 检索项目时遇到错误。有什么方法可以检测邮件是否是邮件,如果不是就直接忽略?以下代码引发 AttributeError: 'MeetingRequest' object has no attribute 'flag')
因为会议请求没有 flag
字段。或者有什么方法可以查看物品的类型吗?
import re
import sys
from exchangelib import DELEGATE, IMPERSONATION, Account, Credentials, ServiceAccount, \
EWSDateTime, EWSTimeZone, Configuration, NTLM, CalendarItem, Message, \
Mailbox, Attendee, Q, ExtendedProperty, FileAttachment, ItemAttachment, \
HTMLBody, Build, Version
from datetime import datetime, timedelta
import pytz
tz = EWSTimeZone.timezone('Asia/Hong_Kong')
creds = Credentials(
username='domain\userID',
password='password')
)
account = Account(
primary_smtp_address='myemail@domain',
credentials=creds,
autodiscover=True,
access_type=DELEGATE)
class Flag(ExtendedProperty):
property_tag = 0x1090
property_type = 'Integer'
Message.register('flag', Flag)
if(len(sys.argv) == 1):
yesterday = tz.localize(EWSDateTime.now() - timedelta(days=3))
today = tz.localize(EWSDateTime.now())
fYear= yesterday.year
fMonth= yesterday.month
fDay= yesterday.day
tYear = today.year
tMonth = today.month
tDay = today.day
elif(len(sys.argv) == 3):
fromDate = sys.argv[1]
toDate = sys.argv[2]
fYear = fromDate[:4]
fMonth = fromDate[4:6]
fDay = fromDate[-2:]
tYear = toDate[:4]
tMonth = toDate[4:6]
tDay = toDate[-2:]
for item in account.inbox.filter(datetime_received__range=(
#tz.localize(EWSDateTime.now() - timedelta(days=1)),
#tz.localize(EWSDateTime.now())
tz.localize(EWSDateTime(int(fYear), int(fMonth), int(fDay))),
tz.localize(EWSDateTime(int(tYear), int(tMonth), int(tDay)))
)):
subA=item.subject
snd=item.sender.email_address
fg=str(item.flag)
str(item.datetime_received.astimezone(pytz.timezone('Asia/Hong_Kong')).strftime("%a %b %d %H:%M:%S %Y"))
rT = str(item.datetime_received.astimezone(pytz.timezone('Asia/Hong_Kong')).strftime("%Y-%m-%d %H:%M:%S"))
cat=str(item.categories)
if not subA:
subA=""
a="\"Inbox\",\""+snd+"\",\""+subA+"\",\""+rT+"\",\""+cat+"\",\""+fg+"\""
print(a)
else:
subA = re.sub('\"\,\"', '\"\,\"', subA.rstrip())
a="\"Inbox\",\""+snd+"\",\""+subA+"\",\""+rT+"\",\""+cat+"\",\""+fg+"\""
decoded = a.encode('utf-8').decode('utf-8')
print(decoded)
只需检查返回项的class:if type(item) == Message
,忽略不匹配的项。
如果想避免错误的原因,还需要在MeetingRequest
class(和MeetingResponse
和 MeetingCancellation
如果你的文件夹中有这些)。
我在使用 exchangelib 检索项目时遇到错误。有什么方法可以检测邮件是否是邮件,如果不是就直接忽略?以下代码引发 AttributeError: 'MeetingRequest' object has no attribute 'flag')
因为会议请求没有 flag
字段。或者有什么方法可以查看物品的类型吗?
import re
import sys
from exchangelib import DELEGATE, IMPERSONATION, Account, Credentials, ServiceAccount, \
EWSDateTime, EWSTimeZone, Configuration, NTLM, CalendarItem, Message, \
Mailbox, Attendee, Q, ExtendedProperty, FileAttachment, ItemAttachment, \
HTMLBody, Build, Version
from datetime import datetime, timedelta
import pytz
tz = EWSTimeZone.timezone('Asia/Hong_Kong')
creds = Credentials(
username='domain\userID',
password='password')
)
account = Account(
primary_smtp_address='myemail@domain',
credentials=creds,
autodiscover=True,
access_type=DELEGATE)
class Flag(ExtendedProperty):
property_tag = 0x1090
property_type = 'Integer'
Message.register('flag', Flag)
if(len(sys.argv) == 1):
yesterday = tz.localize(EWSDateTime.now() - timedelta(days=3))
today = tz.localize(EWSDateTime.now())
fYear= yesterday.year
fMonth= yesterday.month
fDay= yesterday.day
tYear = today.year
tMonth = today.month
tDay = today.day
elif(len(sys.argv) == 3):
fromDate = sys.argv[1]
toDate = sys.argv[2]
fYear = fromDate[:4]
fMonth = fromDate[4:6]
fDay = fromDate[-2:]
tYear = toDate[:4]
tMonth = toDate[4:6]
tDay = toDate[-2:]
for item in account.inbox.filter(datetime_received__range=(
#tz.localize(EWSDateTime.now() - timedelta(days=1)),
#tz.localize(EWSDateTime.now())
tz.localize(EWSDateTime(int(fYear), int(fMonth), int(fDay))),
tz.localize(EWSDateTime(int(tYear), int(tMonth), int(tDay)))
)):
subA=item.subject
snd=item.sender.email_address
fg=str(item.flag)
str(item.datetime_received.astimezone(pytz.timezone('Asia/Hong_Kong')).strftime("%a %b %d %H:%M:%S %Y"))
rT = str(item.datetime_received.astimezone(pytz.timezone('Asia/Hong_Kong')).strftime("%Y-%m-%d %H:%M:%S"))
cat=str(item.categories)
if not subA:
subA=""
a="\"Inbox\",\""+snd+"\",\""+subA+"\",\""+rT+"\",\""+cat+"\",\""+fg+"\""
print(a)
else:
subA = re.sub('\"\,\"', '\"\,\"', subA.rstrip())
a="\"Inbox\",\""+snd+"\",\""+subA+"\",\""+rT+"\",\""+cat+"\",\""+fg+"\""
decoded = a.encode('utf-8').decode('utf-8')
print(decoded)
只需检查返回项的class:if type(item) == Message
,忽略不匹配的项。
如果想避免错误的原因,还需要在MeetingRequest
class(和MeetingResponse
和 MeetingCancellation
如果你的文件夹中有这些)。