azureemailmicrosoft-graph-api

Getting OrganizationFromTenantGuidNotFound error microsoft graph api for sendEmail


Trying to send email using microsoft graph api but getting below error

{"error":{"code":"OrganizationFromTenantGuidNotFound","message":"The tenant for tenant guid '0d41112c-dde8-4984-b7f0-bb346e0560b8' does not exist.","innerError":{"oAuthEventOperationId":"f9cb3ea5-e9db-42fa-a399-bf16784bf768","oAuthEventcV":"4+rYj/snVGgHNU39QbnSrg.1.1","errorUrl":"https://aka.ms/autherrors#error-InvalidTenant","requestId":"748d7aed-9769-4f9a-9fcd-6f3942369fb4","date":"2024-10-24T17:49:25"}}}

$provider = new GenericProvider([
        'clientId' => $config['microsoft']['clientId'],
        'clientSecret' => $config['microsoft']['clientSecret'],
        'redirectUri' => $redirectUrl,
        'urlAuthorize' => $config['microsoft']['loginBaseUrl'] . "/" . $config['microsoft']['tenantId'] . "/oauth2/v2.0/authorize",
        'urlAccessToken' => $config['microsoft']['loginBaseUrl'] . "/" . $config['microsoft']['tenantId'] . "/oauth2/v2.0/token",
        'urlResourceOwnerDetails' => "https://graph.microsoft.com/v1.0/me",
    ]);


    $scope = 'openid email profile https://graph.microsoft.com/.default offline_access'

   $provider->getAuthorizationUrl([
     'scope' => $scope,
     'state' => base64_encode(json_encode($state))
   ]);

Solution

  • As you are sending mail from personal Microsoft account, make sure create app registration with "Account type" as below:

    enter image description here

    Now, I granted Mail.Send API permission of Delegated type in above application like this:

    enter image description here

    Now, I ran below authorization URL in browser and picked personal Microsoft account to login like this:

    https://login.microsoftonline.com/common/oauth2/v2.0/authorize
    ?client_id=appId
    &response_type=code
    &redirect_uri=https://jwt.ms
    &response_mode=query
    &scope=Mail.Send
    &state=12345
    

    enter image description here

    Consent Prompt:

    enter image description here

    After approving the consent, I got code value in address bar as below::

    enter image description here

    In my case, I used Postman to generate access token using authorization code flow with below parameters:

    POST https://login.microsoftonline.com/common/oauth2/v2.0/token
    grant_type:authorization_code
    client_id: <appID>
    client_secret: <secret>
    scope: Mail.Send
    code: <code_from_above>
    redirect_uri: https://jwt.ms
    

    Response:

    enter image description here

    You can now use this access token to send mail from personal Microsoft account user successfully.