Hello folks, is there any solution to add a line break to the custom email notifications template ?
I have the following template:
{{ define "test_tmpl" }}
{{ if gt (len .Alerts.Firing) 0 }}
FIRING : {{ range .Alerts.Firing }}
{{ if gt (len .Annotations) 0 }}
SUMMARY: {{ .Annotations.summary }}
DESCR: {{ .Annotations.description }}
{{ end }}
{{ end }}
{{ end }}
{{ end }}
and I would to add a newline after FIRING and SUMMARY strings. I tried to print a newline character using a Go ‘print’ also html tags and many variants of \n, any attempt doesn’t work. It just print these characters as a plain text, in case with {{print “\n” }} there is no newlines and this command doesn’t print into email as others.
I use Grafana 8.5.5.
@ironbranch there is a fix 9.0.4
Alerting: Preserve new-lines from custom email templates in rendered email by alexweav · Pull Request #52253 · grafana/grafana · GitHub
We’ve also found that different email clients can handle whitespace differently. There is a discussion of that here. The fix in 52253 looks like it incorporates the pre line strategy.
opened 08:46AM - 18 Aug 22 UTC
area/alerting/notifications
**What happened**:
- I modified the `Message` component of a test contact point… to include newlines
- I tested this contact point and read the received email in two different clients:
- Gmail: newlines are preserved
- Outlook (Windows Mail): newlines are not preserved and are replaced with spaces
**What you expected to happen**:
Newlines should be preserved in as many email clients as possible
**How to reproduce it (as minimally and precisely as possible)**:
- Open any Grafana version post 9.0.4 (this test was done on 9.0.7)
- Navigate to Alerts
- Create a new Contact Point, type Email, and add a multi-line message such as
```
a
b
c
```
- Test the contact point and read the email through the two aforementioned clients (Gmail web and Windows Mail desktop)
**Anything else we need to know?**:
- This issue causes the comment over here https://github.com/grafana/grafana/pull/52253#issuecomment-1213333817
- This issue does lie technically on the Windows Mail client, but the support for `white-space` is far from perfect and this means that behavior might vary a lot depending on the user (see https://www.caniemail.com/features/css-white-space/)
- I've dug a bit into this one and found a fix that our team will use locally.
For Windows Mail (and likely most of the Outlook versions that use the Word HTML rendering engine internally) this can be solved by switching to a supported component such as `<pre>`. This doesn't affect templating, as it happens before any values are escaped and rendered.
Here's the modification in `public/emails/ng_alert_notification.html`, replace this section https://github.com/grafana/grafana/blob/fdaf6b5e8d36e726745a77c400f960e24cc4bb72/public/emails/ng_alert_notification.html#L288-L290 with
```html
{{ if gt (len .Message) 0 }}
<div align="left"><pre>{{ .Message }}</pre>
{{ else }}
```
There might be other changes needed to make the `pre` block look more like the text Grafana uses, as most browsers will render this in a monospaced font.
I can do a PR if it makes sense, but I don't know enough about this part of Grafana to understand fully the implications of that change and if that might affect the workflow of other users. Let me know if you think it makes sense.
Thanks for all the hard work on Grafana !
**Environment**:
- Grafana version: 9.x (9.0.4, 9.0.5 and 9.0.7 tested)
- Data source type & version: Not applicable
- OS Grafana is installed on: Tested on `grafana/grafana` Docker container
- User OS & Browser: Linux + Gmail, Windows + Windows Mail
- Grafana plugins: Not applicable
- Others: None