Hi Manik,
When looking at your post, I had something similar, when you receive a webhook notification from the Microsoft Graph API about a new email in your Outlook account, the notification will contain information about the resource that has changed—in this case, an email message.
The resource
field in the webhook payload provides the unique identifier for the email message that was created. To get the full details of this email, you need to make an additional API call to Microsoft Graph to fetch the email using this identifier.
Here's what you need to do after receiving a webhook notification:
- Extract the
resource
field from the webhook payload. This gives you the unique identifier for the email message. - Make a GET request to Microsoft Graph using this identifier to fetch the email message.
Here's an example of how you might do this in your Laravel application: use Illuminate\Support\Facades\Http;
// This is your received webhook payload
$webhookPayload = '{"subscriptionId":"...", "resource":"Users/1371f930512d501f/Messages/AQMkADAwATMwMAItNmM0OS05Yzc5LTAwAi0wMAoARgAAA62kjnAyJSBHg0YiJd35H5EHAJb_1YK2wNxDu6S7OE5bXAMAAAIBDAAAAJb_1YK2wNxDu6S7OE5bXAMAAAAl3HN9AAAA", ...}';
// Decode the JSON payload
$webhookData = json_decode($webhookPayload, true);
// Extract the resource URL from the webhook data
$resourceUrl = $webhookData['resource'];
// Now make a GET request to the Graph API to get the email details
$response = Http::withToken($accessToken)->get("https://graph.microsoft.com/v1.0/{$resourceUrl}");
// Check if the request was successful
if ($response->successful()) {
$emailDetails = $response->json();
// Do something with the email details
// For example, you can log the subject and from address
Log::info('Email received', [
'Subject' => $emailDetails['subject'],
'From' => $emailDetails['from']['emailAddress']['address']
]);
} else {
// Handle the error
Log::error('Failed to fetch email details', [
'status' => $response->status(),
'error' => $response->body(),
]);
}
Make sure to replace $accessToken
with the access token you use to authenticate your requests to the Microsoft Graph API. The access token must have the necessary permissions to read mail in the user's mailbox.
Please note that Microsoft Graph API access tokens typically expire after a short time, so you'll need to handle token refresh as well.
Lastly, make sure that your notification URL endpoint (the webhook receiver in your Laravel application) is secure (uses HTTPS) and is able to validate the notifications coming from Microsoft Graph. This usually involves validating a validationToken
query parameter that Microsoft Graph sends when creating or renewing a subscription.
I hope this helps, if you have any questions don't hesitate to contact me.