postalsys / emailengine

Headless email client
https://emailengine.app/
Other
1.9k stars 169 forks source link

"to" not a supported search term #457

Closed Jivings closed 1 month ago

Jivings commented 1 month ago

The /v1/account/{account}/search endpoint threw this back at me today:

UnsupportedSearchTerm - Unsupported search term "to"

I checked the docs and yes it looks like to is not a possible search term for MS Graph accounts. I checked the API and it looks like to is a possible search param, so is there another reason it's missing?

CleanShot 2024-10-24 at 17 47 47@2x

Often we want to search for a specific to address as our customers have several aliases all landing in the same mailbox.

Thanks!

andris9 commented 1 month ago

Unfortunately, there's no straightforward solution for this issue. The Microsoft Graph API has evolved over time, adding layers with varying properties and capabilities. When searching emails using the MS Graph API, multiple search options exist, each with its own limitations:

In summary, adding support for searching the "to" field in EmailEngine when using the MS Graph API is not easy to do. I have considered implementing an additional search API endpoint in EmailEngine that would use $search instead of $filter, but the limitations would apply: it would return only a single page of results, sorted by relevance.

Jivings commented 1 month ago

I knew there would be a good reason! Thanks for clarifying. I'll have a think about what our options are for this.

Probably the $search option would be better than nothing, I think it will return 1000 results?

andris9 commented 1 month ago

Well, I knew this day would eventually arrive when someone would ask about the to/cc/bcc field searches with MS Graph API. I'll look into adding $search as a selectable option for search queries. The default would be $filter, but you could specify that you want to use $search instead, and in that case, you'd be able to use the to/cc/bcc field (but would lose fields like seen/unseen, etc. that are not supported by $search; also no date based ordering etc.)

Jivings commented 1 month ago

Have you thought about an option that just falls back to IMAP searching if there's an unhandled case like this? I know it would require an extra scope (https://graph.microsoft.com/IMAP.AccessAsUser.All), but that might be preferable for us.

I did check to see if the IMAP proxy worked with OAuth2 provider accounts, just in case 😄 but there's no reason it couldn't be enabled with SASL login for MS accounts right?

EDIT: Not for IDLEing, just for searching

Jivings commented 1 month ago

Or could I register the same account as IMAP and then remove it when the search is done?

Although I think maybe I saw before there's a limitation where I can't add the same account email again with a different provider?

andris9 commented 1 month ago

It's a good idea but, unfortunately, it’s not doable. Technically, IMAP/SMTP scopes fall under the Outlook API, not the MS Graph API. This distinction means that when requesting OAuth2 permissions, you can either use Outlook API-compatible scopes or MS Graph API scopes, but not both together in a functional way.

While it’s technically possible to request both sets of permissions, the access token generated would only be valid for a specific backend—either Outlook API or MS Graph API. If permissions from the incorrect backend are requested, the token validation will fail. Essentially, an access token valid for Mail.* scopes under MS Graph will not work with IMAP*/SMTP* scopes (Outlook API) and vice versa, even if both scopes are requested.

Jivings commented 1 month ago

Damn ok, I'll come back to it then...

andris9 commented 1 month ago

EmailEngine v2.48.4 now supports the $search query. To use it, add useOutlookSearch=true to GET arguments.

curl -X 'POST' 'https://ee.example.com/v1/account/test/search?path=%5CAll&useOutlookSearch=true' \
-H 'Content-Type: application/json'   -d '{
  "search": {
    "to": "recipient@example.com"
  }
}'
Jivings commented 1 month ago

Great, thanks! I'll try it out