Send email via Microsoft Graph with personal account

SMSPGS 20 Reputation points
2024-12-02T23:53:18.49+00:00

I have a small PHP application sending email to users (in my name).

I have a personal MS account and use outlook for emails.

As Microsoft does not support using basic auth to send email through SMTP, I started to look at how to implement OAuth to continue sending emails with my app.

It looks like I have to use MS graph, which I'm fine with, however I can't get it to work.

I registered my app within Entra/Azure AD and succeeded to send an email using graph explorer

If I try to do the same using either PHP (with or without MS SDK), either curl or postman I always get errors. I tried as an app or delegated.

The most recurring one is "The tenant for tenant guid '*****' does not exist" which it seems is because I don't have an office 365 account. I get it when using the tenant id in the authorize url "https://login.microsoftonline.com/{{TENANT}}/oauth2/v2.0/authorize".

I tried using common or consumers instead of my token id and in the first case I can not even login (account not found or similar error) and in the second case I get "unauthorized_client: The client does not exist or is not enabled for consumers"

How do I get a working access token to send an email?

Thanks in advance for any help

Exchange Online
Exchange Online

A cloud-based service included in Microsoft 365, delivering scalable messaging and collaboration features with simplified management and automatic updates.

Outlook | Windows | Classic Outlook for Windows | For business
Microsoft Security | Microsoft Graph
Microsoft Security | Microsoft Entra | Other
0 comments No comments

Answer accepted by question author
Anonymous
2024-12-03T02:23:59.72+00:00

Hello SMSPGS,

Thank you for reaching out to Microsoft Support!

In response to your question, we tested it in postman, and it worked well, so here is our process for getting tokens:

1.Register multi-tenant application with account type as "Accounts in any organizational directory (Any Microsoft Entra ID tenant - Multitenant) and personal Microsoft accounts (e.g. Skype, Xbox)":

enter image description here

2.Make sure to enable public client option if you are using interactive flow for generating token:

enter image description here

3.Add the delegated permission Mail.Send and grant the administrator permission to:

4.Finally, do not use the /tenant-id endpoint to request the token when obtaining the token, as it only supports organizational logins, you should use the /common endpoint to log in to individual accounts.

GET https://login.microsoftonline.com/common/oauth2/v2.0/authorize
POST https://login.microsoftonline.com/common/oauth2/v2.0/token

Reference document:

https://learn.microsoft.com/en-us/graph/auth-v2-user?tabs=http

Hope this helps.

If the answer is helpful, please click Accept Answer and kindly upvote it. If you have any further questions about this answer, please click Comment.

Was this answer helpful?

1 person found this answer helpful.
0 comments No comments

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.