Need help using variables in an Email notification message

Dear Colleagues,

When configuring a contact point of type “Email”, there is a text box for an optional message to include with the Email. How can I use variables in this message (what syntax)? Also, how can I change the Subject of the notification Email?

When I put something like “This is a mail alert {{ .Annotations.summary }} {{ .Annotations.description }}” into the text box, and then send a test notification, the test Email arrives only with the “This is a mail alert” text without any variable expansion. The syntax of what can be used in the “Message” text box should be documented somewhere, with examples? Can you help with the RTFM, or show some examples you personally use there?

grafana-server 8.3.3, Debian 10

Hi @victorsudakov, welcome to the community!

I’m not sure if the Annotations variable can be used in the message body. The docs here say that they can be used in message templates. Those are defined separately, then they can be referenced in that message text box with the special notation {{template "yourTemplateName".}}

The docs have the steps for creating message templates. There are some examples of custom templates too.

Hello @meloriarellano1 !

Thank you for your welcome and reply. It is however not working quite as expected. I define a template:

{{ define "Simple" }}
  [{{.Status}}] {{ .Labels.alertname }}

  Labels:
  {{ range .Labels.SortedPairs }}
    {{ .Name }}: {{ .Value }}
  {{ end }}

  {{ if gt (len .Annotations) 0 }}
  Annotations:
  {{ range .Annotations.SortedPairs }}
    {{ .Name }}: {{ .Value }}
  {{ end }}
  {{ end }}

{{ end }}

Then I refer to this template in my Email contact point and test the contact point. But the resulting Email contains just the Status, no labels or annotations. Where could the rest of the template be? Is anything wrong with the template?

Due to some strange restrictions, I’m not permitted to post more than 1 image, so I had to stitch all the screenshots together, sorry for the inconvenience.

When the template looks like this:

{{ define "Simple2" }}
Simple 2 first line
Labels:
Simple 2 second line

Simple 2 third line
{{ end }}

I get the Emails with the text " Simple 2 first line Labels: Simple 2 second line Simple 2 third line" .

When I add variables and the template becomes like this

{{ define "Simple2" }}
Simple 2 first line
Labels:
Simple 2 second line
{{ range .Labels.SortedPairs }}
    {{ .Name }}: {{ .Value }}
  {{ end }}
Simple 2 third line
{{ end }}

The Email becomes " Simple 2 first line Labels: Simple 2 second line " so everything after the first “{{” is dropped. Why is that?