Create a Draft Reply Email with AI Builder and Graph API in Power Automate

Create a Draft Reply Email with AI Builder and Graph API in Power Automate

6 minutes

The Challenge

Automating responses to emails is straightforward enough in Power Automate Cloud Flows.

Still, sometimes you (or your client or your employer) wants a draft email automatically created that they can review, edit and send from their Outlook.

I’ve had clients request this and I have seen it reported by others on forums that their employers have also asked for it.

With the OOTB actions in Power Automate, we could send an “Start and wait for an approval of text” Approval before the flow sends the email that allows editing of the response text. We could also send a link to a simple Canvas App with the draft email body for review and editing before the user clicks a send button.

Still, the user experience is not the same (or as straightforward) in either of these solutions. Everyone already knows how to view and edit their draft emails in Outlook, why not just stick with that?

The Solution

Use Microsoft Graph APIs with Invoke an HTTP Request

Fortunately, Microsoft Graph REST API has many endpoints for Outlook activities, including creating drafts and replies. We can “Create draft message” and we can even “Create draft to reply”.

POST /users/{id | userPrincipalName}/messages/{id}/createReply        

Example Flow

Create an “HTTP with Microsoft Entra ID (preauthorized)” Connection Reference for Microsoft Graph API

It’s best to create a connection reference in a solution. You can of course directly create a connection in your Power Automate Flow designer, but if you’re using more than one connection for the “Invoke an HTTP request with Entra ID” action it’s best to use connection references.

Name Connection Reference

Give the connection reference a good name so it’s easy to pick the right one.


Article content

Add a new connection to your connection reference

Click “New Connection”


Article content

Select “HTTP with Microsoft Entra ID (preauthorized)


Article content

Add the base URL of Microsoft Graph and login to create the connection


Article content

Create a Simple Triggered by the Arrival of a New Email


Article content

Here my trigger is “When a new email arrives (V3)”.

Create a “Trigger Condition” so that the flow is only triggered by certain emails

You probably don’t want to create draft email responses to every email, especially if you’re using AI Builder credits in the flow. Here for my testing purposes, I have my trigger set up to run on any email from gmail.

I like to use a temporary filter array to help build my trigger conditions. Just drop in references to the relevant trigger properties and build your logic.


Article content

When done, click “Code view” and copy the expression code from the “where” property.


Article content

Paste the expression in a “trigger condition” on your trigger’s settings.


Article content

Delete the temporary filter array.

Save the email address for the reply sender in a compose

This isn’t necessary, but I’m doing it for demonstration purposes.

Here, I’m saving the email address for the user (sender) that will own the draft response. In my case, it’s just my email address. You could source the email address or User ID in a number of different ways, or use the “/me/” Graph Endpoint for your own email account.

Create text with GPT using a prompt with AI Builder

Here I’m using a prompt to read the email subject and body and draft a quick one line response. This is really helpful for loquacious types such as myself, quick responses to longer emails are difficult!

Prompt

Article content


Send the triggering email’s subject and message body to the GPT action


Article content

Build the payload for your Graph API HTTP request

Here I’m constructing the HTTP Request payload in a compose.


Article content

Here is the format for a simple draft email response to the sender of the email. The text output from the AI Builder GPT action above goes in the “message/body/content” property.

{
  "message": {
    "toRecipients": [
      {
        "emailAddress": {
          "address": @{triggerOutputs()?['body/from']}
        }
      }
    ],
    "body": {
      "contentType": "HTML",
      "content": "@{outputs('Create_text_with_GPT_using_a_prompt')?['body/responsev2/predictionOutput/text']}"
    }
  }
}        

Invoke an HTTP request to the Graph API

Finally, we just need to add a preauthorized Entra ID “Invoke an HTTP request” action.


Article content

Set it up as a POST method and construct the relative url of the request.

/v1.0/users/@{outputs('Compose_-_Replying_User_Email')}/messages/@{triggerOutputs()?['body/id']}/createReply        

The key points for the end point are first, the User Id or email address of the user who will be sending the draft reply (you) and the message id of the email that is being replied to (the “id” property from the trigger).


Article content

Finally, just drop in the payload constructed above in the compose action.

Send A Mobile Notification with Link to Draft Email (Optional)

We can also send ourselves a notification on our mobile (assuming we have the Power Automate App installed) with a link to the draft email.

Add a “Send me a mobile notification” action


Article content

Send Link to Draft Email

To send a link to the draft email, reference the “webLink” property of the Graph API call response above.


Article content


Article content

Test flow by sending a long, rambling, unprofessional email from a Gmail account

Use Chat GPT to create an example of a Bad Email

For fun I’m going to test this flow by sending a long, rambling, unprofessional email message to my outlook address from my gmail address. I will use Chat GPT to create the message.


Article content


Send the Email from Gmail to Test Flow

I copied the message Chat GPT created for me and sent it to my Outlook address from my Gmail account.


Article content

The flow runs and creates a short draft response.


Article content

Notification on Mobile

Notification pops up on mobile device.


Article content

Open notification and click on the link to the draft email.


Article content

Review draft email reply in Outlook (OWA)


Article content

Thanks for reading!

If you found this post helpful please give it a like.

Does this respond in thread? I am looking to go to the email in my outlook and see this draft above it in the thread rather than being an email draft not linked to original email.

Like
Reply

To view or add a comment, sign in

More articles by Mark Nanneman

Others also viewed

Explore content categories