Extract multiple lines of text from email using Power Automate

I recently encountered a scenario where I had to extract multiple lines of text from a system generated email using Power Automate. The key to this Flow pattern is the predictability of the system generated message, meaning I can count on line 1 always being line 1 and line 2 always being line 2 etc. The Flow was difficult to create but simple in its final implementation using Html to text and Compose actions. 

In the image below we can see my system generated email. Our Flow will extract lines 1-4. 

System generated email

I am using an Automated cloud flow with the When a new email arrives (V3) trigger. I am using the “Subject Filter” to specify the conditions by which the Flow will execute. In this case any email with a subject of “System email message” will trigger the flow. The context of the trigger is based on the account defined in the connection, i.e. my inbox. 

When a new email arrives

Next, I add an Html to text action and set the content to Body from the When a new email arrives (V3) trigger. 

Html to text

The Html to text action converts the email body (A) into readable and usable content (B). 

Html to text action converts the email body into readable and usable content

Using a Compose action with an expression we extract our target text line.  

Compose action with expression

The expression code is listed below and generally reads as: 

Expression code explanation

Yellow: Replace the hidden line break characters with a pipe symbol using decodeUriComponent(‘%0A’) 

Yellow output

Green: Split Yellow based on the pipe symbol. 

Green output

Purple: Extract the first line ([0]).

Purple output

Expression code:

split(replace(outputs('Html_to_text')?['body'], decodeUriComponent('%0A'), '|'), '|')[0] 

Additional Compose actions are added for each line to be extracted with the only change being the array location value i.e. [1], [2], [3]. 

split(replace(outputs('Html_to_text')?['body'], decodeUriComponent('%0A'), '|'), '|')[1] 
split(replace(outputs('Html_to_text')?['body'], decodeUriComponent('%0A'), '|'), '|')[2] 
split(replace(outputs('Html_to_text')?['body'], decodeUriComponent('%0A'), '|'), '|')[3] 

My completed Flow looks like the image below.  

My completed Flow

I’m sure there are more elegant ways of performing this operation, but it suits my needs and the value it adds outweighs any risk of refactoring if the system generated email ever changes. In my production version of this Flow, data is written to Dataverse for review and action as part of a larger business process and has error handling to let me know if things fail.  

Thanks for reading! 


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s