Search code examples
pythonoutlookmicrosoft-graph-api

Count Outlook Emails Sent to Self via Microsoft Graph API


How does one get the count of Outlook emails sent to self via the Microsoft Graph API Get request?

Among other things, here's what I've tried:

  • Combining $search and $filter (not supported): f"{BASE_URL}/users/{email}/messages?$filter=from/emailAddress/address eq '{email}'&$search=\\"cc:{email}\\""
  • Combining $search and $count (not supported): f"{BASE_URL}/users/{email}/messages?$search=\\"cc:{email}\\"&$count=true"
  • Combining $filter with complex collections (not supported): f"{BASE_URL}/users/{email}/messages?$filter=toRecipients/any(e:e/emailAddress/address eq '{email}')"

I wish to avoid pulling the email details and wrangle upstream as the data contains thousands of emails...


Solution

  • You should be able to use the pidtagmessagerecipientme extended property https://learn.microsoft.com/en-us/office/client-developer/outlook/mapi/pidtagmessagerecipientme-canonical-property which would give you email where your address is in the To or CC line and then also include a from filter to filter to just email that was sent from a particular address eg

    https://graph.microsoft.com/v1.0/me/messages?$filter=singleValueExtendedProperties/any(ep:ep/id eq 'Boolean 0x0059' and cast(ep/value, Edm.Boolean) eq true) and from/emailAddress/address eq '[email protected]'&$count=true