# Need To Parse Email Alerts For Their Data

**URL:** <https://discuss.rapid7.com/t/need-to-parse-email-alerts-for-their-data/4030>\
**Category:** InsightConnect\
**Created:** [March 18, 2021, 1:44pm UTC](https://discuss.rapid7.com/t/need-to-parse-email-alerts-for-their-data/4030 "2021-03-18T13:44:01Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![ilovesoar](https://discuss.rapid7.com/user_avatar/discuss.rapid7.com/ilovesoar/32/358_2.png) [@ilovesoar](https://discuss.rapid7.com/u/ilovesoar)\
**Post date:** [March 18, 2021, 1:44pm UTC](https://discuss.rapid7.com/t/need-to-parse-email-alerts-for-their-data/4030/1 "2021-03-18T13:44:01Z")

</div>

I have emails coming from Absolute DDS and want to parse the fields out of them for automation. Any ideas?

* * *

Identifier: 6abca32a Device Name: [devicenamehere] Username: [test\testy] Department: []\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_

---

<div class="post-metadata">

**Author:** ![joey\_mcadams](https://discuss.rapid7.com/user_avatar/discuss.rapid7.com/joey_mcadams/32/80_2.png) [@joey\_mcadams](https://discuss.rapid7.com/u/joey_mcadams)\
**Post date:** [March 18, 2021, 1:47pm UTC](https://discuss.rapid7.com/t/need-to-parse-email-alerts-for-their-data/4030/2 "2021-03-18T13:47:22Z")

</div>

Can you get those from the headers array?

If not, is that the absolute string you’re trying to parse?

---

<div class="post-metadata">

**Author:** ![elijah\_martin-merrill](https://discuss.rapid7.com/user_avatar/discuss.rapid7.com/elijah_martin-merrill/32/109_2.png) [@elijah\_martin-merrill](https://discuss.rapid7.com/u/elijah_martin-merrill)\
**Post date:** [March 18, 2021, 2:11pm UTC](https://discuss.rapid7.com/t/need-to-parse-email-alerts-for-their-data/4030/3 "2021-03-18T14:11:36Z")

</div>

This looks like parsing data from the body of the e-mail. I’d do it in two passes:

1. parse the body of the e-mail with the pattern-match step capturing the entire entry like so:  
`{{items:/Identifier:\s+\S+\s+Device Name:\s+\S+\s+Username:\s+\S+\s+Department:\s+\S+\s+}}`  
and make sure you have selected “return multiple matches”. This should cause the pattern-match step to produce an array of items. Loop over this array.  
Inside the loop, parse out the individual fields with another pattern-match step:  
`Identifier:\s+{{identifier:/\S+/}}\s+Device Name:\s+[{{device_name:/\S+/}}]\s+Username:\s+[{{username:/\S+/}}]\s+Department:\s+[{{department:/\S+/}}]`  
and do not select return multiple matches. This should give you 4 strings output from the pattern match step: identifier, device\_name, username, and department  
Now you have the 4 fields parsed out into variables you can proceed as normal.

---

<div class="post-metadata">

**Author:** ![elijah\_martin-merrill](https://discuss.rapid7.com/user_avatar/discuss.rapid7.com/elijah_martin-merrill/32/109_2.png) [@elijah\_martin-merrill](https://discuss.rapid7.com/u/elijah_martin-merrill)\
**Post date:** [March 18, 2021, 2:13pm UTC](https://discuss.rapid7.com/t/need-to-parse-email-alerts-for-their-data/4030/4 "2021-03-18T14:13:35Z")

</div>

Note: this regex was developed against the sample in the original message which MAY NOT BE ACCURATE. I’d recommend having ICON trigger on one of these e-mails, copying the body output from the trigger into [regex101.com](http://regex101.com), and develop/test the regex there per [Developing and Debugging Regex for the Pattern Match Step](https://discuss.rapid7.com/t/developing-and-debugging-regex-for-the-pattern-match-step/3636)

---

<div class="post-metadata">

**Author:** ![joey\_mcadams](https://discuss.rapid7.com/user_avatar/discuss.rapid7.com/joey_mcadams/32/80_2.png) [@joey\_mcadams](https://discuss.rapid7.com/u/joey_mcadams)\
**Post date:** [March 18, 2021, 3:04pm UTC](https://discuss.rapid7.com/t/need-to-parse-email-alerts-for-their-data/4030/5 "2021-03-18T15:04:03Z")

</div>

I’d use a python step for this, I’m not a fan of regex.

```auto
input = "Identifier: 6abca32a Device Name: [devicenamehere] Username: [test\testy] Department: [] _______________________________________________________________"

def run(params={}):
    input = params.get("input")
    split_string = input.split(":")
    output_dict = {}
    for s in split_string:
        split_element = s.strip().split(" ")
        if len(split_element) > 1:
            output_dict[split_element[0]]=" ".join(split_element[1:])

    return output_dict

print(run({"input":input}))

```

You can get more elaborate and strip the “[” “]” out off the keys as well.

---

<div class="post-metadata">

**Author:** ![elijah\_martin-merrill](https://discuss.rapid7.com/user_avatar/discuss.rapid7.com/elijah_martin-merrill/32/109_2.png) [@elijah\_martin-merrill](https://discuss.rapid7.com/u/elijah_martin-merrill)\
**Post date:** [March 18, 2021, 5:12pm UTC](https://discuss.rapid7.com/t/need-to-parse-email-alerts-for-their-data/4030/6 "2021-03-18T17:12:07Z")

</div>

The regex does this as well - it all depends on where you put the `[` in the pattern!

If you do it with a python step, don’t forget to match up the step output schema to match your output object so you can use it in the variable picker later.

---

<div class="post-metadata">

**Author:** ![ilovesoar](https://discuss.rapid7.com/user_avatar/discuss.rapid7.com/ilovesoar/32/358_2.png) [@ilovesoar](https://discuss.rapid7.com/u/ilovesoar)\
**Post date:** [March 19, 2021, 9:17pm UTC](https://discuss.rapid7.com/t/need-to-parse-email-alerts-for-their-data/4030/7 "2021-03-19T21:17:21Z")

</div>

maybe I am making a simple mistake here… it is friday afternoon.

function:def run(params={}):  
input = params.get(“input”)  
split\_string = input.split(":")  
output\_dict = {}  
for s in split\_string:  
split\_element = s.strip().split(" “)  
if len(split\_element) \> 1:  
output\_dict[split\_element[0]]=” ".join(split\_element[1:])

```
return output_dict

```

print(run({“input”:input}))  
input:Identifier: 6abca32a Device Name: [devicenamehere] Username: [test\testy] Department: []

and ERRRORRRRR

(‘action input JSON was invalid’, \<ValidationError: “‘Identifier: 6abca32a Device Name: [devicenamehere] Username: [test\\testy] Department: []’ is not of type ‘object’”\>)  
Traceback (most recent call last):  
File “/usr/local/lib/python3.7/site-packages/komand-1.0.1-py3.7.egg/komand/plugin.py”, line 380, in start\_step  
step.input.validate(params)  
File “/usr/local/lib/python3.7/site-packages/komand-1.0.1-py3.7.egg/komand/variables.py”, line 24, in validate  
validate(parameters, self.schema)  
File “/usr/local/lib/python3.7/site-packages/jsonschema-2.3.0-py3.7.egg/jsonschema/validators.py”, line 432, in validate  
cls(schema, \*args, \*\*kwargs).validate(instance)  
File “/usr/local/lib/python3.7/site-packages/jsonschema-2.3.0-py3.7.egg/jsonschema/validators.py”, line 117, in validate  
raise error  
jsonschema.exceptions.ValidationError: ‘Identifier: 6abca32a Device Name: [devicenamehere] Username: [test\testy] Department: []’ is not of type ‘object’

Failed validating ‘type’ in schema[‘properties’][‘input’]:  
{‘description’: 'Input object to be passed as `params={}` to the `run` ’  
‘function’,  
‘order’: 2,  
‘title’: ‘Input’,  
‘type’: ‘object’}

On instance[‘input’]:  
('Identifier: 6abca32a Device Name: [devicenamehere] Username: ’  
‘[test\testy] Department: []’)

During handling of the above exception, another exception occurred:

Traceback (most recent call last):  
File “/usr/local/lib/python3.7/site-packages/komand-1.0.1-py3.7.egg/komand/plugin.py”, line 311, in handle\_step  
output = self.start\_step(input\_message[‘body’], ‘action’, logger, log\_stream, is\_test, is\_debug)  
File “/usr/local/lib/python3.7/site-packages/komand-1.0.1-py3.7.egg/komand/plugin.py”, line 387, in start\_step  
raise ClientException(’{} input JSON was invalid’.format(step\_key), e)  
komand.exceptions.ClientException: (‘action input JSON was invalid’, \<ValidationError: “‘Identifier: 6abca32a Device Name: [devicenamehere] Username: [test\\testy] Department: []’ is not of type ‘object’”\>)

---

<div class="post-metadata">

**Author:** ![ilovesoar](https://discuss.rapid7.com/user_avatar/discuss.rapid7.com/ilovesoar/32/358_2.png) [@ilovesoar](https://discuss.rapid7.com/u/ilovesoar)\
**Post date:** [March 19, 2021, 9:18pm UTC](https://discuss.rapid7.com/t/need-to-parse-email-alerts-for-their-data/4030/8 "2021-03-19T21:18:09Z")

</div>

I failed on the first pass, no matches for me.

---

<div class="post-metadata">

**Author:** ![tyler\_schmidtke](https://discuss.rapid7.com/user_avatar/discuss.rapid7.com/tyler_schmidtke/32/226_2.png) [@tyler\_schmidtke](https://discuss.rapid7.com/u/tyler_schmidtke)\
**Post date:** [March 19, 2021, 9:36pm UTC](https://discuss.rapid7.com/t/need-to-parse-email-alerts-for-their-data/4030/9 "2021-03-19T21:36:41Z")

</div>

> [@ilovesoar](#):
>
> Identifier: 6abca32a Device Name: [devicenamehere] Username: [test\testy] Department:

@ilovesoar, can you try this in pattern match?

```auto
Identifier: {{identifier:/([^\s]*)/}} Device Name: {{device_name:/\[([^\]]*)\]/}} Username: {{username:/\[([^\]]*)\]/}} Department: {{department:/\[([^\]]*)\]/}}

```

![image](https://forum-uploads-hub-prod-1-us-east-1-rapid7-com.s3.dualstack.us-east-1.amazonaws.com/original/1X/5fbc59721765144d53771bb4db616b6642e84da3.png)  
 ![image](https://forum-uploads-hub-prod-1-us-east-1-rapid7-com.s3.dualstack.us-east-1.amazonaws.com/original/1X/5f7b6d7ac903ef20cd9e85ac97f2463bf0ac1377.png)

---

<div class="post-metadata">

**Author:** ![joey\_mcadams](https://discuss.rapid7.com/user_avatar/discuss.rapid7.com/joey_mcadams/32/80_2.png) [@joey\_mcadams](https://discuss.rapid7.com/u/joey_mcadams)\
**Post date:** [March 22, 2021, 2:22pm UTC](https://discuss.rapid7.com/t/need-to-parse-email-alerts-for-their-data/4030/10 "2021-03-22T14:22:22Z")

</div>

Just use this:

```
def run(params={}):
    input = params.get("input")
    split_string = input.split(":")
    output_dict = {}
    for s in split_string:
        split_element = s.strip().split(" ")
        if len(split_element) > 1:
            output_dict[split_element[0]]=" ".join(split_element[1:])

    return output_dict

```

I think you may have included the print statement at the end which will cause that to fail. I left it there for debugging if you’re writing this outside of ICON in a python editor of some sort.

The error I believe is telling you it’s trying to parse that print statement.

---

<div class="post-metadata">

**Author:** ![ilovesoar](https://discuss.rapid7.com/user_avatar/discuss.rapid7.com/ilovesoar/32/358_2.png) [@ilovesoar](https://discuss.rapid7.com/u/ilovesoar)\
**Post date:** [April 2, 2021, 6:23pm UTC](https://discuss.rapid7.com/t/need-to-parse-email-alerts-for-their-data/4030/11 "2021-04-02T18:23:37Z")

</div>

Identifier: 6abca32a Device Name: [devicenamehere] Username: [test@test.com] Department: []

So found out sometimes it is like this and it breaks the regex!

---

<div class="post-metadata">

**Author:** ![tyler\_schmidtke](https://discuss.rapid7.com/user_avatar/discuss.rapid7.com/tyler_schmidtke/32/226_2.png) [@tyler\_schmidtke](https://discuss.rapid7.com/u/tyler_schmidtke)\
**Post date:** [April 2, 2021, 6:43pm UTC](https://discuss.rapid7.com/t/need-to-parse-email-alerts-for-their-data/4030/12 "2021-04-02T18:43:51Z")

</div>

@ilovesoar, I’m having trouble figuring out what about that is breaking it. It looks like the only thing different is that `@` in the username. Each of the`[^\]]` should ensure that everything is matched that is not a closing square bracket. Testing on Regex 101 matches it successfully. (Sorry if I missed some other difference)

Link to the regex match below:  
[https://regex101.com/r/21hO9m/1](https://regex101.com/r/21hO9m/1)
