From b6bc0647cdfeadfb5cb7551eec72c7575514484b Mon Sep 17 00:00:00 2001
From: lwandzura <51929958+lwandzura@users.noreply.github.com>
Date: Mon, 28 Mar 2022 15:11:20 -0500
Subject: [PATCH] Add to template (#3886)
* added TICK script example
* add toTemplate info and example
---
.../kapacitor/v1.6/event_handlers/email.md | 38 ++++++++++++++++++-
1 file changed, 37 insertions(+), 1 deletion(-)
diff --git a/content/kapacitor/v1.6/event_handlers/email.md b/content/kapacitor/v1.6/event_handlers/email.md
index c3c3a631a..c269c347e 100644
--- a/content/kapacitor/v1.6/event_handlers/email.md
+++ b/content/kapacitor/v1.6/event_handlers/email.md
@@ -75,8 +75,9 @@ The following Email event handler options can be set in a
`.email()` in a TICKscript.
| Name | Type | Description |
-| ---- | ---- | ----------- |
+| ---- | --------------- | ------------------------ |
| to | list of strings | List of email addresses. |
+| toTemplate(s) | string template| Derived email addresses. |
### Example: handler file
```yaml
@@ -187,3 +188,38 @@ Add the handler:
```bash
kapacitor define-topic-handler email_cpu_handler.yaml
```
+
+### Send email alerts using the toTemplate option
+
+You can use `toTemplate` to derive email addresses directly from data instead of hardcoding them individually.
+In the example below, we are using both the `to` option and `toTemplates` option in order to derive email addresses from a dataset and send email alerts directly to recipients.
+Like the `to` option, the `toTemplates` option can be used more than once in a TICKscript.
+You can combine the `to` and `toTemplates` options or use them individually depending on your use case.
+
+```js
+stream
+ |from()
+ .measurement('cpu')
+ .where(lambda: "host" == 'serverA')
+ .groupBy('host')
+ |window()
+ .period(10s)
+ .every(10s)
+ |count('value')
+ |default()
+ .field('extraemail','bob@example.com')
+ .tag('tagemail','bob2@example.com')
+ |alert()
+ .id('kapacitor.{{ .Name }}.{{ index .Tags "host" }}')
+ .details('''
+{{.Message}}
+Value: {{ index .Fields "count" }}
+Details
+''')
+ .info(lambda: "count" > 6.0)
+ .warn(lambda: "count" > 7.0)
+ .crit(lambda: "count" > 8.0)
+ .email()
+ .to('user1@example.com', 'user2@example.com')
+ .toTemplates('{{ index .Fields "extraemail" }}')
+```
\ No newline at end of file