Microsoft Flow – parse email and extract information

If you have a 3rd party system that sends standardized notifications and you would like to automatically process those notifications, then this Flow can be helpful. The flow below parses a predefined email, extracts information for you to take further actions with it.

Those actions can be:

  • saving data into a SharePoint List;
  • forwarding part of an email to another system;
  • removing all formatting and so on.

The flow is pretty simple and consists of these steps:

1) Trigger – When a new email arrives.

2) Action – Html to text – convert email body from HTML to plain text.

3) Action – Initialize variable – store a carriage return value in it. Peek code view:

{
    "inputs": {
        "variables": [
            {
                "name": "CRLF",
                "type": "String",
                "value": "\n"
            }
        ]
    }
}

4) Action – Compose – split output from #2 into an array. Expression:

split(
    trim(
        uriComponentToString(
            replace(
                uriComponent(
                    body('Convert_email_Html_to_Text')
                ),
                '%0A',
                '~~'
            )
        )
    ),
    '~~'
)

5) Action – Filter – remove empty lines from #4. Code for Edit in advanced mode:

@not(equals(item(), ''))

6) Action – Filter – find all lines that start with NAME word. Code for Edit in advanced mode:

@startsWith(item(), 'NAME')

7) Action – Filter – find all lines that start with EMAIL word. Code for Edit in advanced mode:

@startsWith(item(), 'EMAIL')

8) Action – Filter – find all lines that start with PHONE word. Code for Edit in advanced mode:

@startsWith(item(), 'PHONE')

9) Action – Compose – combine and output #6, #7, #8. Expression:

concat(
	body('Get_NAME_line')?[0],
	variables('CRLF'),
	body('Get_EMAIL_line')?[0],
	variables('CRLF'),
	body('Get_PHONE_line')?[0]
)

Please note how I’ve used three different ways to show the code as one works better than the other in each situation – Expression, Edit in advanced mode, and Peek code.


RESULT

This is how a sent message looks like – it has an image, a bit of formatting, some extra info (that should not be processed per the flow):

And here is the result of the flow only extracting what it needed to:

0

Leave a Reply

Close Menu