Send Email from PHP Using Microsoft Graph API (Microsoft 365)

Send Email from PHP Using Microsoft Graph API (Microsoft 365)

This guide explains how to:

  1. Authenticate user with Microsoft 365
  2. Get Access Token
  3. Send Email using /me/sendMail
  4. Use pure PHP (cURL)
  5. No SDK required

Prerequisites

  1. Before starting, make sure:
  2. App registered in Azure Portal
  3. Redirect URI added
  4. API Permissions (Delegated):
    1. User.Read
    2. Mail.Send
    3. offline_access
  5. Admin Consent granted (if required)
  6. Click Here For Azure Portal Setup

Project Structure

/send_email/
  1. login.php
  2. callback.php
  3. send_mail.php

STEP 1: login.php

<?php
session_start();

$tenantId = "YOUR_TENANT_ID";
$clientId = "YOUR_CLIENT_ID";
$redirect = "callback.php";

$scope = urlencode("User.Read Mail.Send offline_access");

$state = bin2hex(random_bytes(16));
$_SESSION['oauth_state'] = $state;

$authUrl ="https://login.microsoftonline.com/$tenantId/oauth2/v2.0/authorize?"
. "client_id=$clientId"
. "&response_type=code"
. "&redirect_uri=" .urlencode($redirect)
."&response_mode=query"
. "&scope=$scope"
. "&state=$state";

header("Location: $authUrl");
exit;
?>

STEP 2: callback.php


<?php
session_start();

$tenantId ="YOUR_TENANT_ID";
$clientId ="YOUR_CLIENT_ID";
$clientSecret = "YOUR_CLIENT_SECRET";
$redirect ="http://localhost/send_email/callback.php";

if (!isset($_GET['code'])) {
die("Authorization codemissing");
}

if ($_GET['state'] !== $_SESSION['oauth_state']) {
die("Invalid state");
}

$code = $_GET['code'];

$tokenUrl ="https://login.microsoftonline.com/$tenantId/oauth2/v2.0/token";

$postData = [
"client_id" => $clientId,
"client_secret" =>$clientSecret,
"grant_type" => "authorization_code",
"code" => $code,
"redirect_uri" =>$redirect,
"scope" => "User.Read Mail.Sendoffline_access"
];

$ch = curl_init($tokenUrl);
curl_setopt_array($ch, [
CURLOPT_POST => true,
CURLOPT_POSTFIELDS => http_build_query($postData),
CURLOPT_RETURNTRANSFER => true
]);

$response = curl_exec($ch);
curl_close($ch);

$data = json_decode($response, true);

if (!isset($data['access_token'])) {
die("Token error: " .$response);
}

$_SESSION['access_token'] =$data['access_token'];
$_SESSION['refresh_token'] = $data['refresh_token'];

header("Location: send_mail.php");
exit;
?>

STEP 3: send_mail.php

<?php
session_start();

if (!isset($_SESSION['access_token'])) {
die("Not authenticated");
}

$accessToken = $_SESSION['access_token'];

$graphUrl = "https://graph.microsoft.com/v1.0/me/sendMail";

$emailData = [
"message" => [
"subject" =>"Hello from Microsoft Graph API",
"body" => [
"contentType" =>"HTML",
"content" =>"<h2>Email Sent Successfully</h2>"
],
"toRecipients" => [
[
"emailAddress"=> [
"address"=> "recipient@example.com"
]
]
]
],
"saveToSentItems" =>true
];

$ch = curl_init($graphUrl);
curl_setopt_array($ch, [
CURLOPT_POST => true,
CURLOPT_POSTFIELDS => json_encode($emailData),
CURLOPT_RETURNTRANSFER => true,
CURLOPT_HTTPHEADER => [
"Authorization: Bearer$accessToken",
"Content-Type:application/json"
]
]);

$response = curl_exec($ch);
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);

if ($httpCode === 202) {
echo "Email sentsuccessfully!";
} else {
echo "Failed: " .$response;
}
?>


    • Related Articles

    • How to configure or access SYSMIC’s IceWarp Email ?

      To access Email through webmail browse through the following URL: sysmic.icewarpcloud.in To configure MS Outlook or another email client (using POP3/IMAP4) use the following settings: Step 1: IMAP4 Server – sysmic.icewarpcloud.in Step 2: IMAP4 Ports: ...