Parsing Email Bodies Using Power Automate
    • Dark
      Light
    • PDF

    Parsing Email Bodies Using Power Automate

    • Dark
      Light
    • PDF

    Article Summary

    Power Automate includes the ability to filter emails by configuring settings in the trigger such as the person who sent the email or the ability to look for keywords within the subject. But, what happens if we want to look for a specific keyword inside the body of the email? For example, a retailer who sells sporting equipment may be looking for emails that is about a hat.

    However, the problem is not that simple. There are two other requirements that we have to deal with including:

    • Words within a word (i.e. hat appears in many other words: chat, that, thatcher etc)
    • Words can be provided in many different cases (Hat, HAT, hat)

    To address these scenarios, we will use the contains expression to determine if the word ‘hat’ exists but we will need to extend it to address situations related to case and to ensure that the word ‘hat’ is not nested.

    Let’s now go ahead and build our solution by adding the following triggers/actions to our flow:

    • An Office 365 Outlook trigger that includes a subject filter of order.
    • Since most emails are sent in HTML format, we can use the Html to text action to remove all of the HTML markup from our email message. We will provide the Body attribute from our trigger as our input to this action.
    • To assist with debugging, we will add a Compose action and then add our Html to text action output as the input for this action.

    1-trigger(4)

    • Now we will focus on parsing our message body and will use a Condition to assist with this. Inside our condition, we will leverage an expression to convert our email message body to upper case so that we will not have to worry about how the email author has casted the word. To assist with this we will use the toUpper(triggerBody()?['body']) expression.
    • We will then determine if the result of our toUpper expression contains the word _HAT.

    Note: _ represents a space and not an underscore. Including it for illustration purposes. Also note that depending upon your word, you may want to include a _ space after the word as well if your word also represents the start of a longer word. For example, hat is the beginning of the word hate.
    2-condition

    • We will add some Mobile notification actions. These are just for testing purposes so that we can easily determine if our email body contains the word hat.

    Testing

    Let’s start with our happy path and send an email that includes the word hat in lower case.
    3-happypath

    When our flow processes this request, we will determine that hat has been detected and that we have processed the email correctly.
    4-happypathresult

    We will now send in an email that has the word hat embedded in another word. In this case we want to exclude this email from further processing.
    5-negativepath

    When we process the email this time, we can detect that the word hat is embedded in another word (that) and as a result we can ignore this email from future processing.

    netativepathresult


    Was this article helpful?