Sending E-Mails using Microsoft 365 via SMTP

Christian Becker 21 Reputation points
2024-08-24T12:16:00.8366667+00:00

Hi There,

I have a plain old php application hosted on azure. It uses SMTP for sending emails.

I have also a MS 365 Subscription and the email address, that should be used for sending the E-Mails doesn't even have a password. Sure I could create one, but this would lead to other problems as app passwords are not longer supported.

What is the best way to send E-Mails from the azure hosted app using my domain?

Bests
Chris

Microsoft 365
Microsoft 365
Formerly Office 365, is a line of subscription services offered by Microsoft which adds to and includes the Microsoft Office product line.
5,125 questions
Microsoft Exchange
Microsoft Exchange
Microsoft messaging and collaboration software.
565 questions
Azure App Service
Azure App Service
Azure App Service is a service used to create and deploy scalable, mission-critical web apps.
7,913 questions
{count} votes

1 answer

Sort by: Most helpful
  1. Yasir Nawaz 5 Reputation points
    2024-08-24T12:55:07.43+00:00

    To send emails from your Azure-hosted PHP application using Microsoft 365, follow these simple steps using Microsoft Graph API:

    1. Register Your Application in Azure

    • Go to Azure Portal: Visit Azure Portal.
    • Create an App Registration:
      • Navigate to Azure Active Directory > App registrations.
      • Click New registration and follow the prompts to create an application.
      • Save the Application (client) ID and Directory (tenant) ID that you get.

    2. Set Up API Permissions

    • Add Permissions:
      • In your app registration, go to API permissions > Add a permission.
      • Choose Microsoft Graph > Delegated permissions.
      • Add Mail.Send to allow sending emails.

    3. Generate a Client Secret

    • Create a Secret:
      • Go to Certificates & secrets > New client secret.
      • Describe the secret and set an expiration time.
      • Click Add and save the secret value. This is your password.

    4. Get an Access Token

    • Request an Access Token:
      • Make a POST request to the token URL:
            POST https://login.microsoftonline.com/{tenant}/oauth2/v2.0/token
        
      • Use these parameters in your request:
            grant_type=client_credentials
            client_id={client_id}
            client_secret={client_secret}
            scope=https://graph.microsoft.com/.default
        
      • The response will include an access token you’ll use for authentication.

    5. Send an Email

    • Use Microsoft Graph API:
      • Make a POST request to:
            POST https://graph.microsoft.com/v1.0/me/sendMail
        
      • Include the access token in the Authorization header:
            Authorization: Bearer {access_token}
        
      • Send the email content in JSON format:
            {
              "message": {
                "subject": "Test Email",
                "body": {
                  "contentType": "Text",
                  "content": "This is a test email."
                },
                "toRecipients": [
                  {
                    "emailAddress": {
                      "address": "recipient@example.com"
                    }
                  }
                ]
              }
            }
        
    1 person found this answer helpful.
    0 comments No comments

Your answer

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