This guide explains how to:
- Authenticate user with Microsoft 365
- Get Access Token
- Send Email using /me/sendMail
- Use pure PHP (cURL)
- No SDK required
Prerequisites
- Before starting, make sure:
- App registered in Azure Portal
- Redirect URI added
- API Permissions (Delegated):
- User.Read
- Mail.Send
- offline_access
- Admin Consent granted (if required)
- Click Here For Azure Portal Setup
Project Structure
/send_email/
- login.php
- callback.php
- 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;
}
?>