The Microsoft Power Automate service allows you to create automated workflows between apps and services. In this article, we will use three examples to demonstrate how you can process emails with Power Automate.

Email alerts, repetitive emails, emails from certain people. We all deal with email on a day-to-day basis. If you are in an MSP environment these might be ticket alerts or alerts from services or software you supply and manage.

Traditionally, you could use Microsoft Outlook mailbox rules to process these messages, deciding whether the content met certain criteria and then performing an action. Those actions were limited, but they served a purpose; for example, they quickly sorted emails from backup software for successful or failed backups, saving you time from having to skim through messages you may not need to action.

Your PSA or ticketing system may even accept new tickets that are raised by email, so a rule that forwarded a failure alert to the PSA to generate a ticket was a good way to automate something that would otherwise have been farmed out to one of the junior techs.

Of course, these methods still work and are easily reproduceable in Power Automate. However, the Power in Power Automate comes not just from reading the text in an email and performing an action; it is the other actions we can build into our flows.

Let's have a look at the examples.

Example 1: Read and find text in an email ^

When you work with alerts from software or services, you may notice that they are all formatted in the same way. Here is an example of an alert from a Unifi Wireless Controller. As you can see, we have the controller name/URL and then a nicely formatted block of text showing us the device name, ID, and the site it is from.

Alert from a Unifi Wireless Controller

Alert from a Unifi Wireless Controller

With traditional Outlook rules, we can look for text in this message and then perform our action; this requires a different rule for each action we want to perform. With Power Automate, we can build a complex flow based on multiple criteria.

The first thing we will do is convert the body of the email from HTML to plain text. Start a new automated cloud flow, choosing When an Email Arrives as a trigger.

Choosing when an Email arrives as a trigger

Choosing when an Email arrives as a trigger

If this is the first email flow trigger you have used, you will need to sign in to create a connection to Outlook. You can also sign into other mailboxes, for example, if you have a dedicated mailbox for receiving alerts.

Open the advanced options, and in the From field, enter the address of the account that sends the alerts you are working with.

Email flow advanced options

Email flow advanced options

Choose to Add a new step, then search for Content Conversion and select the HTML to Text action.

HTML Content conversion step

HTML Content conversion step

In the HTML to Text action window, select the body area. From the Dynamic Content menu, select Body.

Dynamic content from email

Dynamic content from email

Email body for conversion

Email body for conversion

The dynamic content is pulled from the email trigger; triggers are predefined properties and content from the message that will arrive. If this flow were triggered now, the output of the flow would simply be the body of the email converted to plain text.

Body converted to plain text

Body converted to plain text

Add a new step. This time, search for Compose; you will find it under Data Operation.

Compose Data Operation

Compose Data Operation

Click Inputs, and then switch to the Expression tab. The expression language can be a bit confusing. It is documented here. I find I still spend many hours scratching my head about why or why not an expression works as expected.

We are going to use the split expression, which works the same as it does in many other languages. You choose a block of text or array to split and an operator to split on.

In this step, we want to collect the device name. Using the plain text output from the previous step, we know that the device name is between device name and device ID:

unifi.mydomain.com: Switch Disconnected

 	 
  	  	  	
  	Device Name: 	SW01	 
Device ID: 	00:00:00:01
Site: 	Head Office	 
Message: 	Switch was disconnected

We can accomplish that split in several steps. First, we split the text on "Device Name:"

Split(body('Html_to_text'), 'Device Name:')

Doing this creates two pieces of text, 0 and 1. 0 is the left side of the split and 1 is the right side of the split.

Left side:

unifi.mydomain.com: Switch Disconnected

 	 
  	  	  	
  	Device Name:

We want to use the right side of the split, which in our example now looks like this:

	SW01	 
Device ID: 	00:00:00:01
Site: 	Head Office	 
Message: 	Switch was disconnected

As you can see, it literally splits on the given string, but doesn't do any sort of tidying up so we are left with a tab indent/white space before the device name.

We can then split this again using 'Device ID.' In that case, we will choose the left side of the split, which will leave us with the device name:

Split(Split(body('Html_to_text'), 'Device Name:')[1], 'Device ID:')[0]

This leaves us with:

	SW01	

We can then add a trim to remove any remaining white space from the text string.

trim(Split(Split(body('Html_to_text'), 'Device Name:')[1], 'Device ID:')[0])

This leaves us with:

SW01
Compose operation with expression

Compose operation with expression

Compose operation with expression

Using the ellipses on the Compose step, you can rename this to Device Name.

Rename the step

Rename the step

If you run this flow now, you will output the device name of the disconnected device.

You can repeat this process to create compose steps for each piece of text you want to work with from the email body. You can also apply the same actions on the email subject.

Rather than simply checking that an email contains certain keywords, you can now grab text out of emails, store it, and start to work with it.

Example 2: Use found text from an email ^

Now that we have collected text from our email alert, what are we going to do with it? Well, the answer is pretty much whatever you want. Maybe you are tracking alerts and want to store them in a table. Configure an Add Row to Table Action for Excel, link up to where your file is stored (OneDrive or SharePoint), and choose the file. Power Automate will read the file and find all the tables stored in it.

Excel add row to table

Excel add row to table

Choose the column in which to store your data.

Excel add row to table flow

Excel add row to table flow

If the flow runs, it will take our captured data and add it to our table. We might only want to add to the table if the device is from the Head Office, which we can do with a condition.

Add a row to the table if the site is the head office

Add a row to the table if the site is the head office

You can choose to email someone, you can create an item in SharePoint, or you can use an API (if you have a premium license). The possibilities are not endless, but close.

Example 3: Using variables ^

You can see from the example images that they become difficult to work with. When you look at the flow, you cannot easily identify which output is which. Power Automate supports the use of variables, which makes working with text a lot easier.

Choose to add an action, search for Initialize Variable, set the type as String, and enter a name. You can also rename the action to help identify it in your flow.

You can initialize the variable empty, or you can set a default value. These support dynamic content and expressions in the same way compose data operations do; however, you can also set variables later in flows using conditions and other determinations you might make.

Initialize a variable

Initialize a variable

Use a variable

Use a variable

You can see the difference in the above example between using a compose data operation and a named variable.

Subscribe to 4sysops newsletter!

I hope these examples have demonstrated how you can take traditional Outlook-style mailbox rules and start to apply some real power to them.

+2
avatar
1 Comment
  1. Saurabh Suman 5 months ago

    how to read an email body if the email is received in Text format. Converting text to HTML changes the alignment:

    eg

    Actual Email (text) 

    A

    B

    C

    post the html to text conversion:

    A B 

    C

     

     

    0

Leave a reply

Please enclose code in pre tags

Your email address will not be published. Required fields are marked *

*

© 4sysops 2006 - 2021

CONTACT US

Please ask IT administration questions in the forums. Any other messages are welcome.

Sending

Log in with your credentials

or    

Forgot your details?

Create Account