More flexibility in email alert template (MIME, etc)

Bug #1622593 reported by Vegard Vesterheim
6
This bug affects 1 person
Affects Status Importance Assigned to Milestone
Network Administration Visualized
New
Undecided
Unassigned

Bug Description

Alert mail sent by NAV is MIME-type text/plain. It would be useful to also allow HTML-formatted mail, preferably as an alternative to text-formatting, aka multipart/alternative.

One possibility is to consider the mail template as a RFC822-formatted message, containing both headers and body, separated by a blank line. The Django templating system makes this a bit cumbersome, because the handling of white-space and linefeeds is geared towards HTML-output.

In the current implementation, the Subject header must be the first line. A simplistic solution to the more general approach above, could therefore be to allow another header line, namely "Content-type" as the *second* line in the template.

Revision history for this message
Vegard Vesterheim (vegard-vesterheim) wrote :

root@nav:/usr/share/pyshared/nav/alertengine/dispatchers# diff -u email_dispatcher.py~ email_dispatcher.py
--- email_dispatcher.py~ 2016-06-30 10:46:58.000000000 +0200
+++ email_dispatcher.py 2016-09-12 14:36:30.390898104 +0200
@@ -39,15 +39,24 @@
         # Remove the subject line
         message = '\n'.join(message.splitlines()[1:])

+ content_type='text/plain'
+ # Look for Content-type in next line
+ template_content_type = message.splitlines(1)[0].lstrip('Content-Type:').strip()
+ if template_content_type:
+ content_type = template_content_type
+ # Remove the Content-type line
+ message = '\n'.join(message.splitlines()[1:])
+
         headers = {
             'X-NAV-alert-netbox': alert.netbox,
             'X-NAV-alert-device': alert.device,
             'X-NAV-alert-subsystem': alert.source,
+ 'Content-type': content_type,
         }

         try:
             if not address.DEBUG_MODE:
- email = EmailMessage(subject=subject, body=message,
+ email = EmailMessage(subject=subject, body=message, headers=headers,
                                      to=[address.address])
                 email.send(fail_silently=False)
             else:

Revision history for this message
Vegard Vesterheim (vegard-vesterheim) wrote :

This will break old templates!

How should we preserve backwards compatibility?

Revision history for this message
Vegard Vesterheim (vegard-vesterheim) wrote :

Idea from John-Magne: How about reusing the HTML-templates from the web-gui in the email templates

description: updated
To post a comment you must log in.
This report contains Public information  
Everyone can see this information.

Other bug subscribers

Remote bug watches

Bug watches keep track of this bug in other bug trackers.