如何使用 Python 的 O365 库获取特定电子邮件中的完整收件人列表?
How do I get the full list of recipients in a specific email with Python's O365 library?
我需要从我有权访问的帐户中检索电子邮件。我有两个条件:
- 电子邮件必须未读。
- “收件人”、“抄送”和“密件抄送”字段可能不包含特定地址:“name@example.com”。
未读过滤器 (1) 按预期工作,但对于 (2),我了解到无法为“收件人”/“抄送”/“密件抄送”字段添加过滤器(请参阅 ).为了解决这个问题,我检索了所有未读的电子邮件,然后尝试在我的代码中处理额外的过滤。
但是,似乎只有一个选项可以使用 get_first_recipient_with_address()
获取“收件人”字段中的第一个地址。我需要完整的列表才能检查!我在文档中找不到其他方法
>>> from O365 import Account, FileSystemTokenBackend
# Authenticating the account works fine
>>> credentials = ("client ID", "client secret")
>>> token_backend = FileSystemTokenBackend(token_path='.', token_filename='o365_token.txt')
>>> account = Account(credentials, token_backend=token_backend)
# Making a new query for the inbox:
>>> mailbox = account.mailbox()
>>> inbox = mailbox.inbox_folder()
>>> query = mailbox.new_query()
# Filtering for unread works as expected:
>>> query.on_attribute('isRead').equals(False)
>>> unread_messages = inbox.get_messages(limit=1, query=query)
# Did not succeed in adding a "to" filter to `query`,
# so I want to filter on attributes of the returned emails:
>>> for mess in unread_messages:
... recipient = mess.to.get_first_recipient_with_address()
... print(recipient.address)
...
>>> "some_other_address@example.com"
我知道最后打印的地址是相关电子邮件“收件人”字段中的第一个地址,但我需要完整列表,它应该是 ["some_other_address@example.com", "name@example.com"]
有什么想法或解决方法吗?
我通过仔细观察 source code 找到了一种方法。有一个隐藏变量 _recipients
,您可以使用它来获取完整列表,如下所示:
>>> unread_messages = inbox.get_messages(limit=1, query=query)
>>> for mess in unread_messages:
... recipients = [rec.address for rec in mess.to._recipients]
... print(recipients)
...
>>> ["some_other_address@example.com", "name@example.com"]
我需要从我有权访问的帐户中检索电子邮件。我有两个条件:
- 电子邮件必须未读。
- “收件人”、“抄送”和“密件抄送”字段可能不包含特定地址:“name@example.com”。
未读过滤器 (1) 按预期工作,但对于 (2),我了解到无法为“收件人”/“抄送”/“密件抄送”字段添加过滤器(请参阅
但是,似乎只有一个选项可以使用 get_first_recipient_with_address()
获取“收件人”字段中的第一个地址。我需要完整的列表才能检查!我在文档中找不到其他方法
>>> from O365 import Account, FileSystemTokenBackend
# Authenticating the account works fine
>>> credentials = ("client ID", "client secret")
>>> token_backend = FileSystemTokenBackend(token_path='.', token_filename='o365_token.txt')
>>> account = Account(credentials, token_backend=token_backend)
# Making a new query for the inbox:
>>> mailbox = account.mailbox()
>>> inbox = mailbox.inbox_folder()
>>> query = mailbox.new_query()
# Filtering for unread works as expected:
>>> query.on_attribute('isRead').equals(False)
>>> unread_messages = inbox.get_messages(limit=1, query=query)
# Did not succeed in adding a "to" filter to `query`,
# so I want to filter on attributes of the returned emails:
>>> for mess in unread_messages:
... recipient = mess.to.get_first_recipient_with_address()
... print(recipient.address)
...
>>> "some_other_address@example.com"
我知道最后打印的地址是相关电子邮件“收件人”字段中的第一个地址,但我需要完整列表,它应该是 ["some_other_address@example.com", "name@example.com"]
有什么想法或解决方法吗?
我通过仔细观察 source code 找到了一种方法。有一个隐藏变量 _recipients
,您可以使用它来获取完整列表,如下所示:
>>> unread_messages = inbox.get_messages(limit=1, query=query)
>>> for mess in unread_messages:
... recipients = [rec.address for rec in mess.to._recipients]
... print(recipients)
...
>>> ["some_other_address@example.com", "name@example.com"]