Trigger Webhook when an email is received

Manik Mia 20 Reputation points
2024-02-21T05:13:21.6166667+00:00

In my laravel application I want when somebody send any mail to my microsoft outlook account It will send a post notification so that I can understand somebody mailed me. That's why I am subscribing microsoft webhook notfication bellow my php code.

$subscriptionData = [ 'changeType' => 'created', 'notificationUrl' => route('microsoft.email.webhook'), 'resource' => "/me/mailfolders('inbox')/messages", 'expirationDateTime' => Carbon::now()->addMinutes(10060)->toAtomString(),];$subscriptionResponse = Http::withToken($accessToken)->post('https://graph.microsoft.com/v1.0/subscriptions', $subscriptionData);

This code is totally correct. Its subscribing microsoft webhook notification and its send post notification to my laravel application. And Request Data is

{"subscriptionId":"2b274e49-04e7-41c5-8247-cc344e4531c2","subscriptionExpirationDateTime":"2024-02-27T13:40:41+00:00","changeType":"created","resource":"Users/1371f930512d501f/Messages/AQMkADAwATMwMAItNmM0OS05Yzc5LTAwAi0wMAoARgAAA62kjnAyJSBHg0YiJd35H5EHAJb_1YK2wNxDu6S7OE5bXAMAAAIBDAAAAJb_1YK2wNxDu6S7OE5bXAMAAAAl3HN9AAAA","resourceData":{"@odata.type":"#Microsoft.Graph.Message","@odata.id":"Users/1371f930512d501f/Messages/AQMkADAwATMwMAItNmM0OS05Yzc5LTAwAi0wMAoARgAAA62kjnAyJSBHg0YiJd35H5EHAJb_1YK2wNxDu6S7OE5bXAMAAAIBDAAAAJb_1YK2wNxDu6S7OE5bXAMAAAAl3HN9AAAA","@odata.etag":"W/\"CQAAABYAAACW/tWCtsDcQ7ukuzhOW1wDAAAl2KgF\"","id":"AQMkADAwATMwMAItNmM0OS05Yzc5LTAwAi0wMAoARgAAA62kjnAyJSBHg0YiJd35H5EHAJb_1YK2wNxDu6S7OE5bXAMAAAIBDAAAAJb_1YK2wNxDu6S7OE5bXAMAAAAl3HN9AAAA"},"clientState":null,"tenantId":null}

Sometimes I need Which resource has created for this webhook post request. How can I find resource of this post request sometimes I need full mail of post request.

What can I do? Please Help me.

Microsoft Security Microsoft Graph
0 comments No comments
{count} votes

Accepted answer
  1. RevelinoB 3,675 Reputation points
    2024-02-21T05:38:10.9633333+00:00

    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:

    1. Extract the resource field from the webhook payload. This gives you the unique identifier for the email message.
    2. 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.

    1 person found this answer helpful.
    0 comments No comments

0 additional answers

Sort by: Most helpful

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.