Microsoft Flow – parse email and extract information

  • Post Category:Flow
  • Post Comments:22 Comments
3+

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:

3+

This Post Has 22 Comments

  1. Ben Hiron-Grimes

    thank you very much for this, very useful
    When I test it I get an error on step 4:

    BadRequest. The ‘from’ property value in the ‘query’ action inputs is of type ‘String’. The value must be an array.

    please advise

    0
    1. Pavel Bludov

      Hi Ben,
      I will need more info, so I am about to email you.
      Thanks!

      0
  2. Hunter

    I could use some help as well, Im dealing with something similar.

    0
    1. Pavel Bludov

      Hi,
      Sure, I can try to help – about to email you.

      1+
  3. Amandaoldham

    I did trial and error for 10 hours. This article finally helped. Thanks!

    0
    1. Pavel Bludov

      Hi Amanda(?),
      Thanks for stopping by, good to hear!

      0
  4. Steven

    Hey Pavel,
    Do you know a way to extract a particular value from a message in teams.
    MessageCards do not allow a mention to work so I was thinking of parsing out a persons name between two values <> and then having a flow mention that NAME in the same channel to say there is a message they need to look at. The messages arrive via the O365 connector and it HAS to use MessageCards.

    0
    1. Pavel Bludov

      Hi Steven,
      Did you mean an Adaptive Card?
      Can you also please give more details on your case – how you would like it to work. Not sure I understood the concept.
      Thanks!

      0
  5. Karl

    Hi Pavel,

    I got stuck at 3 Action – Initialize variable – store a carriage return value in it. Peek code view:

    The Peek code view wouldn’t allow me to edit the code – I presume I need to install an editor?

    0
    1. Pavel Bludov

      Hi Karl,
      You will be surprised how trivial that step and value is. What you do is put your mouse cursor into that value field, press Enter on the keyboard so it adds the line/carriage return. That is it.
      Thanks!

      0
  6. Mikola

    On number 4 I got an error, the same one as Ben Hiron-Grimes, has anyone found a solution for that issue?

    0
    1. Pavel Bludov

      Hey Mikola,
      Replying to your email for more details.
      Thanks!

      0
  7. Kevin

    I am also having the same error on step 4, as Ben and Mikola

    0
    1. Pavel Bludov

      Hi Kevin,
      Mikola had other issues and I believe he is all set.
      Step 4 is where you split your data into an array. As long as your HTML to Text works well (check the output of it) and CRLF is correctly initialized (it’s just a carriage return stored in it), then Step 4 should have no errors.
      Also make sure that step is Compose type action.

      0
  8. Franky

    Hello and thank you for your article. It is right what I wanted.

    I tried couple of time but could not complete it. 🙁

    Step 4 a 5 !!

    0
    1. Pavel Bludov

      Hi Franky,
      I’ll send you an email to get more details. Otherwise, hard to troubleshoot.
      Thanks!

      0
  9. Ram Soni

    I got the same error on number 4 as Ben Hiron-Grimes mentioned. Could you help me on this?

    0
    1. Pavel Bludov

      Hi Ram,
      I think most if not all issues have been resolved posted in the comments. So I’m about to send you an email for more details.
      Thanks!

      0
  10. Marcel

    ;Hi Pavel,

    I recon i doing something wrong but don’t know what. I cant get step 4 right when i add the compose it gives me inputs and i do not see how to change that in splits or something.

    0
    1. Pavel Bludov

      Hi Marcel,
      I’ll reply through an email.
      Thanks for checking my site.

      0
  11. Emilio

    I was just looking for a way to identify an email address in a forewarded message so that I can take it and work with it. Your post sets me in the right direction..Thanks. It is not pretty but it works.
    As far as you know, is there a way to identify strings through regular expressions? It would have been way more easier that way.

    0
    1. Pavel Bludov

      Hi Emilio,
      I wish there were a better way but it looks like currently it’s either using these not so pretty workarounds or using 3rd party actions (e.g. Plumsail).
      Thanks!

      0

Leave a Reply