# Transform email body response to a more readable form

**URL:** https://discuss.elastic.co/t/transform-email-body-response-to-a-more-readable-form/227889
**Category:** Elasticsearch
**Created:** [April 14, 2020, 10:38am UTC](https://discuss.elastic.co/t/transform-email-body-response-to-a-more-readable-form/227889 "2020-04-14T10:38:51Z")
**Posts on this page:** 7
**Page:** 1

<div class="post-metadata">

### Author: ![Alexandros888](https://avatars.discourse-cdn.com/v4/letter/a/3ec8ea/32.png) [@Alexandros888](https://discuss.elastic.co/u/Alexandros888)
#### Post date: [April 14, 2020, 10:38am UTC](https://discuss.elastic.co/t/transform-email-body-response-to-a-more-readable-form/227889/1 "2020-04-14T10:38:52Z")

</div>

Hello,

I have created the following watcher alert:

```
    {
      "trigger": {
        "schedule": {
          "hourly": {
            "minute": [
              1,
              3,
              11,
              16,
              23,
              31,
              41,
              51
            ]
          }
        }
      },
      "input": {
        "search": {
          "request": {
            "search_type": "query_then_fetch",
            "indices": [
              "simulation-connect"
            ],
            "rest_total_hits_as_int": true,
            "body": {
              "query": {
                "bool": {
                  "must": [
                    {
                      "match_phrase": {
                        "customerNr_onBehalf": {
                          "query": "1234"
                        }
                      }
                    },
                    {
                      "range": {
                        "@timestamp": {
                          "gte": "now-50m"
                        }
                      }
                    }
                  ],
                  "filter": [
                    {
                      "bool": {
                        "should": [
                          {
                            "bool": {
                              "should": [
                                {
                                  "match": {
                                    "resultAddArticleToBasket": true
                                  }
                                }
                              ],
                              "minimum_should_match": 1
                            }
                          },
                          {
                            "bool": {
                              "should": [
                                {
                                  "bool": {
                                    "should": [
                                      {
                                        "match": {
                                          "resultArticleSearch": true
                                        }
                                      }
                                    ],
                                    "minimum_should_match": 1
                                  }
                                },
                                {
                                  "bool": {
                                    "should": [
                                      {
                                        "bool": {
                                          "should": [
                                            {
                                              "match": {
                                                "resultFullTextSearch": true
                                              }
                                            }
                                          ],
                                          "minimum_should_match": 1
                                        }
                                      },
                                      {
                                        "bool": {
                                          "should": [
                                            {
                                              "bool": {
                                                "should": [
                                                  {
                                                    "match": {
                                                      "resultLogout": true
                                                    }
                                                  }
                                                ],
                                                "minimum_should_match": 1
                                              }
                                            },
                                            {
                                              "bool": {
                                                "should": [
                                                  {
                                                    "bool": {
                                                      "should": [
                                                        {
                                                          "match": {
                                                            "resultLogin": true
                                                          }
                                                        }
                                                      ],
                                                      "minimum_should_match": 1
                                                    }
                                                  },
                                                  {
                                                    "bool": {
                                                      "should": [
                                                        {
                                                          "match": {
                                                            "resultVehicleSearch": true
                                                          }
                                                        }
                                                      ],
                                                      "minimum_should_match": 1
                                                    }
                                                  }
                                                ],
                                                "minimum_should_match": 1
                                              }
                                            }
                                          ],
                                          "minimum_should_match": 1
                                        }
                                      }
                                    ],
                                    "minimum_should_match": 1
                                  }
                                }
                              ],
                              "minimum_should_match": 1
                            }
                          }
                        ],
                        "minimum_should_match": 1
                      }
                    }
                  ]
                }
              }
            }
          }
        }
      },
      "condition": {
        "compare": {
          "ctx.payload.hits.total": {
            "gte": "1"
          }
        }
      },
      "actions": {
        "send_email": {
          "email": {
            "profile": "standard",
            "to": [
              "alexandros.ananikidis@sag-ag.ch"
            ],
            "subject": "[CH PROD] Connect CH Customer Simulation Failed",
            "body": {
              "text": "Elastic results are the following: {{#ctx.payload.hits.hits}}{{_source}}{{/ctx.payload.hits.hits}}"
            }
          }
        }
      }
    }

```

And the output is correctly like that:

 ![image](https://us1.discourse-cdn.com/elastic/original/3X/0/0/008bfeed13164d31ac6538620d30ab390cb6fc6a.png)

Nevertheless, because as anyone can imagine it is extremely inconvenient to read how can i change the code in my alert so i can have at the end an email notification that will show the info in a more easy and clear way to read?

For example like that:

The results are the following:

Hit 1

resultLogin=true,  
@timestamp=1586857855,  
resultVehicleSearch=true,  
resultArticleSearch=true,  
resultFullTextSearch=true,  
sessionID=455108a4054a4c2a93fa23cba4bc85c4,  
customerNr\_onBehalf=1234,  
resultAddArticleToBasket=true,  
resultLogout=true

Hit 2

resultLogin=true  
@timestamp=1586858457,  
resultVehicleSearch=true,  
resultArticleSearch=true,  
resultFullTextSearch=true,  
sessionID=63c8aebbd39842398afa7b7399025526,  
customerNr\_onBehalf=1234,  
resultAddArticleToBasket=true,  
resultLogout=true

Hit 3....and so on

---

<div class="post-metadata">

### Author: ![Luca\_Belluccini](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/luca_belluccini/32/33239_2.png) [@Luca\_Belluccini](https://discuss.elastic.co/u/Luca_Belluccini)
#### Post date: [April 14, 2020, 11:26pm UTC](https://discuss.elastic.co/t/transform-email-body-response-to-a-more-readable-form/227889/2 "2020-04-14T23:26:00Z")

</div>

Hello @Alexandros888

Watcher can send emails in `plaintext` or `HTML`.  
You can use the `Mustache` language to format your email.

In `plaintext`, you can use this snippet in your action.  
Please consider this is not valid JSON, the triple quotes `"""` can be interpreted by Kibana Dev Tools and will be converted to valid JSON:

```auto
"subject": "[CH PROD] Connect CH Customer Simulation Failed",
  "body": {
    "text": """Elastic results are the following:

{{#ctx.payload.hits.hits}}
resultlogin={{_source.resultLogin}}
resultVehicleSearch={{_source.resultVehicleSearch}}
resultArticleSearch={{_source.resultArticleSearch}}
@timestamp={{_source['@timestamp']}}
{{/ctx.payload.hits.hits}}"""
  }

```

In `html`:

```auto
"subject": "[CH PROD] Connect CH Customer Simulation Failed",
  "body": {
    "html": """<h1>Elastic results are the following</h1>
<table style="width:100%">
  <tr>
    <th>resultLogin</th>
    <th>resultVehicleSearch</th>
    <th>resultArticleSearch</th>
    <th>@timestamp</th>
  </tr>
{{#ctx.payload.hits.hits}}
<tr>
    <td>{{_source.resultLogin}}</td>
    <td>{{_source.resultVehicleSearch}}</td>
    <td>{{_source.resultArticleSearch}}</td>
    <td>{{_source['@timestamp']}}</td>
</tr>
{{/ctx.payload.hits.hits}}
</table>"""
  }

```

---

<div class="post-metadata">

### Author: ![Alexandros888](https://avatars.discourse-cdn.com/v4/letter/a/3ec8ea/32.png) [@Alexandros888](https://discuss.elastic.co/u/Alexandros888)
#### Post date: [April 15, 2020, 9:11am UTC](https://discuss.elastic.co/t/transform-email-body-response-to-a-more-readable-form/227889/3 "2020-04-15T09:11:22Z")

</div>

Luca you are AWESOME thank you very much for your solution!!!!.Much appreciated.  
Just a final question if you have time, can you also tell me how to add borders and color in my table?

Thank you very much

---

<div class="post-metadata">

### Author: ![Luca\_Belluccini](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/luca_belluccini/32/33239_2.png) [@Luca\_Belluccini](https://discuss.elastic.co/u/Luca_Belluccini)
#### Post date: [April 15, 2020, 9:36am UTC](https://discuss.elastic.co/t/transform-email-body-response-to-a-more-readable-form/227889/4 "2020-04-15T09:36:12Z")

</div>

Yes, it is possible to use HTML attributes or CSS styles.

See [Watcher “watcher.actions.email.html.sanitization” table attributes](https://discuss.elastic.co/t/watcher-watcher-actions-email-html-sanitization-table-attributes/50971)  
E.g.

```auto
<table border='1' style='font-family:sans-serif;font-size:13px'>

```

Just pay attention to the HTML sanitization.  
Elasticsearch will filter some HTML tags or elements.  
If you see some elements are not present in the final email, it means you have to allow the element in sanitization using [those settings](https://www.elastic.co/guide/en/elasticsearch/reference/current/actions-email.html#email-html-sanitization).

---

<div class="post-metadata">

### Author: ![Alexandros888](https://avatars.discourse-cdn.com/v4/letter/a/3ec8ea/32.png) [@Alexandros888](https://discuss.elastic.co/u/Alexandros888)
#### Post date: [April 16, 2020, 2:07pm UTC](https://discuss.elastic.co/t/transform-email-body-response-to-a-more-readable-form/227889/5 "2020-04-16T14:07:48Z")

</div>

Hello Luca thanks for all the valuable info really appreciated great help provided.  
If you have some time check whenever you can also that link where i have a question similar to that.  
The issue there is how to create an html like the one that you created here but in second or even third level aggregation.

Relative link: [Make the email body of my alert easier to read](https://discuss.elastic.co/t/make-the-email-body-of-my-alert-easier-to-read/224773/7)

In any case thanks again for that one 🙂

---

<div class="post-metadata">

### Author: ![Luca\_Belluccini](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/luca_belluccini/32/33239_2.png) [@Luca\_Belluccini](https://discuss.elastic.co/u/Luca_Belluccini)
#### Post date: [April 16, 2020, 10:22pm UTC](https://discuss.elastic.co/t/transform-email-body-response-to-a-more-readable-form/227889/6 "2020-04-16T22:22:18Z")

</div>

I've answered to the linked question.  
Please mark this question as solved if you think this has been solved.

---

<div class="post-metadata">

### Author: ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)
#### Post date: [May 14, 2020, 10:22pm UTC](https://discuss.elastic.co/t/transform-email-body-response-to-a-more-readable-form/227889/7 "2020-05-14T22:22:19Z")

</div>

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.
