Microsoft Flow Send Email from HTTP Request

Quickly create your own simple API to send email as you

NaS IT
NaS IT
Sep 19, 2018 · 6 min read

If you have to send email from your automation script (most likely PowerShell) using your Office 365 mailbox (Exchange Online), there is a simple way to do that using Microsoft Flow.

We can create a 2 steps (trigger + action) flow that will allow you to send email quite securely (no need to handle password in script).

So to do this you need to have your email provided by Office 365, if it’s the case you should also have access to Flow (Office 365 tier).


Create the Flow

Head over to Microsoft Flow.

Click Create from blank.

Create a new Flow

Again create from blank

Create from blank

To select your trigger, type in http and click on Request — When a HTTP request is received.

Select HTTP trigger

You will get something like this, and be asked to define your JSON schema for the HTTP request.

HTTP request trigger definition

In this case we just want to have 3 fields:

  • To
  • Subject
  • Body

As its’ all we will need to put in the send email action.

The JSON schema can be easily created from a sample payload.

Use sample payload

Click on Use sample payload to generate schema and paste in the JSON as below.

{"to": "","subject": "","body": ""}

Click OK and the JSON schema will be generated for you.

Generated JSON Schema

Then click on Show advanced options.

Advanced options

Select POST as method.

Select method

We are done with the HTTP trigger. Now click on + New step and click on Add and action.

Add an action

You should see Office 365 Outlook — Send an email action being listed, select it.

Add the Send an email action

We will now configure the send an email action with the values fetched from the HTTP request.

Send an email required fields

Click in the To field, for some reason the dynamic content is not showing up directly, we have to click See more.

Dynamic content selection

You should be able to select to.

Dynamic content from HTTP request

And proceed the same for the 2 other fields.

All email fields with content from the HTTP request

Then click Show advanced options. You can set the importance of the email (by default low), and also define set Is HTML to yes if your body is in HTML format.

Advanced options

That’s all, click Save.

Save the flow

At this point we are still missing the URL that we could use to do our POST request.

Click on the HTTP request header to expand it and click save again.

HTTP POST URL missing

Now you should have the URL that you can copy (the URL will still be showing next time you edit the flow).


Test the Flow

When you are editing your flow, you can test it. Click on the Test icon in the upper right corner.

Test Flow

Select I’ll perform the trigger action.

Test Flow with trigger action

Flow is now waiting for you to perform the trigger action.

Flow waiting for the trigger

To test that you can use your favorite REST API client (like Postman) but I like to do everything with PowerShell and in the end it’s in this context that I want to use this flow.

So first let’s put the the request body in a variable.

$body = @{"subject"="Test";"body"="<p>This is a test from Flow HTTP request</p>";"to"="me@contoso.com"} | ConvertTo-Json

Then the Flow HTTP URL (replace with the URL generated previously).

$uriFlow = "https://*****************"

And then we use the Invoke-RestMethod cmdlet.

Invoke-RestMethod -Method Post -Uri $uriFlow -Body $body -ContentType "application/json"

Apparently the Flow HTTP endpoint doesn’t return anything, so if you get something, you can assume it’s an error (for your error handling).

Flow result

Both steps should have the green check mark.

If you have an error, click on the step to expand and see the details. Note that if any of the 3 email fields are missing, this will result in an error of Send an email action.


Security and Flow life cycle

First this should be fairly secure at the condition that you keep the endpoint URL safe (for example, don’t put it in your code and push to GitHub…).

If you need to regenerate periodically the HTTP endpoint URL, you can just copy the existing flow and you will get a new URL.

Save As
Create a copy of a Flow

Also one thing I have not shown here when creating the flow, is the connection to Office 365 Outlook. If it’s the first time you add an Office 365 Outlook action, you will have to initiate the connection (need to sign-in).

Normally Flow handles refresh tokens and authentication things behind the scene for you, but you must make sure that your connection is still healthy from time to time. You can check that from Flow Settings.

Flow Settings
Flow Connections
Flow connections list and status

However, normally you will get email alerts when your flows are failing (at least a weekly digest).


For more information on Microsoft Flow usage, please refer to the official documentation.

And if you need more info on Microsoft Flow pricing and plans (including a Free plan).

https://flow.microsoft.com/en-us/pricing/

Hopefully this article could be helpful to you, and demonstrate quickly a pattern that can be re-used in many occasion for your automation scripts, especially when you want to interact with Office 365 services in some way (Outlook, SharePoint…).

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade