Extract “From” Address from Forwarded Email in Power Automate & Logic Apps.

Scenario: You have a flow that picks up emails that have been forwarded to an inbox, and you want to capture the original sender name and email address, rather that those of the person who forwarded the mail.

For example you could have a person monitoring an inbox for particular types of emails, then have a Quick Step in Outlook they can press when they identify certain characteristics in the email, which forwards it to a mailbox to be picked up by a Flow to process.

Firstly, trigger on when a new email arrives then put the body through HTML to text:

Next, you want the positions in the text where the end of the string From: exists, and the beginning of Sent:. We use the indexOf function for this, and in the case of From: add 6 to the result, as indexOf gets the beginning of the search string and 6 is the length of “From: ” including the space.

Initialise a couple of string variables to hold the From name and From Email. You could do this right at the beginning if you want, it doesn’t matter, as long as it’s before the condition that comes next.

Now add a condition that evaluates whether we actually found From: and Sent: in the email in the right order. If the mail wasn’t forwarded, or if the person who forwarded the mail stripped out the quoted text, it could be missing. If it’s missing and we try to run the message body through the next series of formulae the flow will fail and we need to handle that properly.

We do this by evaluating of the index of Sent: is greater than the index of From:. If not, then set those two variables to something that indicates no email address was found. If yes, then we do the heavy lifting in a Compose and two Set variable actions:

Here are the formulas for the three steps within the If yes branch:

Extract Email:

trim(
	substring(
		body('Body_to_text'),
		add(
			indexOf(
				body('Body_to_text'),
				'From:'),
			6),
		sub(
			indexOf(
				body('Body_to_text'),
				'Sent:'),
			add(
				indexOf(
					body('Body_to_text'),
					'From:'),
				6)
			)
		)
	)

Set strEMail parsed:

trim(
	substring(
		last(
			split(
				outputs('Extract_Email'),
				'<')
			),
		0,
		sub(
			length(
				last(
					split(
						outputs('Extract_Email'),
						'<')
					)
				),
			1)
		)
	)

set strFromName parsed:

trim(
	substring(
		first(
			split(
				outputs('Extract_Email'),
				'<')
			),
		0,
		sub(
			length(
				first(
					split(
						outputs('Extract_Email'),
						'<')
					)
				),
			1)
		)
	)

You may notice the only difference between the two formulas for the Name and Email is whether we take the first or last item of the array generated by splitting outputs(‘Extract_Email’) on the < character.

In the first formula (Extract Email) the substring() function is getting everything starting from the 6 characters after the start of ‘From:’ and ending at the start of the string ‘Sent:’. Substring() requires the text in which to find the substring as the first input parameter, then the starting index (the number of characters from the start of the input string) as an integer number, and then finally the length of the substring.

Obviously in this case we don’t know how far into the quoted email we’ll find the email address and we don’t know how long the email address will be, so we need to find these values using indexOf and in the case of ‘From:’ add 6 to it to get the end of the search string not the start.

This formula outputs everything, trimmed of leading or trailing spaces, between the end of ‘From: ‘ and the beginning of ‘Sent:’, which is where the email address appears when you forward something in Outlook:

The next two formulas split the sender’s display name and the senders email address.

That’s done by using the split() function. Split() takes the string as its first input and the string to split on as its second input. The output is an array with the number of items corresponding to one more than the number of times the delimiter appears in the text.

Running the highlighted string through split() would result in an array that looks like this:

[
"Joe Blogs ",
"joe.blogs@example.co.nz>"
]

The rest of the formulae is selecting which item in the array we’re interested in with first() or last(), then just trimming the last character off with substring(). One of the formulas takes the first item in the array and the second one takes the last item in the array, but they’re otherwise identical.

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