How is it possible to filter on `author` or `sender` using Python exchangelib v 4.5.1?

Viewed 309

Using exchangelib's django-like syntax, I can successfully filter() on a message's subject field:

my_account.inbox.filter(subject__contains="foo")

But I've found no way to do (and no examples of) a filter() on sender or author. Since author (for example) is a Mailbox object with a name field, I expected this, or some variant on it to work:

my_account.inbox.filter(author__name="Foo")

But it fails with an error like this:

exchangelib.fields.InvalidField: Unknown field path 'author__name' on folders 
[Messages(Root(<exchangelib.account.Account object at 0x0000020561F64308>, ....

Variants like sender__name__eq, sender__email_address, etc. fared no better.

I've also tried an undecorated sender. Using sender doesn't give an error, but it never seems to match anything (at least not the name or email_address fields).

What am I missing?

1 Answers

The logic for filtering on the author and sender fields in EWS are a bit weird. There's a long explanation at https://github.com/ecederstrand/exchangelib/issues/913. The main part to take away is:

  • You cannot filter on subfields of sender. sender__name and sender__email cannot be referenced in a filter expression in EWS
  • sender__icontains filter works as expected when mailbox_type='OneOff', not mailbox_type='Mailbox'
  • using an AQS query string like .filter('from:"somedomain.com"') works as expected for both OneOff and Mailbox mailbox types
Related