Home / How to create custom email template in Alertmanager?

How to create custom email template in Alertmanager?

Configure your email receiver to, for example, display the email's subject in the headers section and its template-based body of text.

receivers:
- name: EmailReceiver
- to: myaddress@example.com
headers:
subject: 'Summary: {{ .CommonLabels.alertname }} {{ if eq .Status "firing" }}(Firing){{ else }}(Resolved){{ end }}'
html: '{{ template "emailcontent.txt" . }}'

As indicated below, add the section templates to your alertmanager.yaml configuration file.

 templates:
- /etc/alertmanager/config/custom_mail.tmpl

Mount the custom mail.tmpl file in the alertmanager pod at /etc/alertmanager/config. Please take note that the define clause should exactly match the email recipient's html section:

{{ define ""emailcontent.txt" }} ... <content of the email> ... {{ end}}

Mount this file in the alertmanager pod, at /etc/alertmanager/config. Get the base64 encoded content of the file.

cat custom_mail.tmpl | base64 -w 0

Insert this in the alertmanager-main secret.

oc edit -n openshift-monitoring edit secret alertmanager-main

Add the below section.

apiVersion: v1
data:
alertmanager.yaml: Imdsb2JhbCI6CiAgInJlc29sdmVfdGltZW91dCI6ICI1bSIK....
custom_mail.tmpl: <content from former step>

check the file has been mounted in alertmanager pod.

oc -n openshift-monitoring exec alertmanager-main-0 -- ls -l /etc/alertmanager/config
total 0
lrwxrwxrwx. 1 root nobody 24 Oct 18 14:59 alertmanager.yaml -> ..data/alertmanager.yaml
lrwxrwxrwx. 1 root nobody 15 Oct 18 14:59 custom_mail.tmpl -> ../data/custom_mail.tmpl

Leave a Reply