Repository: getredash/redash

Issue:
Gmail is merging [OK] and [Triggered] alert emails
### Issue Summary

In Gmail the alert emails from redash are merged on the same thread, which makes it hard to know if an alert was triggered or solved.

Here is the explanation of the behavior of Gmail:
https://www.quora.com/Why-when-I-edit-subject-in-email-and-add-the-symbols-at-the-beginning-of-the-current-subject-to-keep-email-in-the-same-thread-the-email-sometimes-get-divided-in-a-new-thread-and-sometimes-doesnt/answer/Vineet-Chawla
### Steps to Reproduce
1. Setup an alert, register to receive email updates on a gmail address
2. Trigger the alert
3. Change the query and re-run it so that redash sends the email starting with [OK]
4. In gmail you will see the two emails under the subject starting with [Triggered] like this screenshot: ![screenshot from 2016-06-19 14 24 07](https://cloud.githubusercontent.com/assets/494686/16177359/7c8ddc8a-362a-11e6-8d80-f13202143ea2.png)
5. The expectation is to have two threads, or to have the information about whether the alert was triggered or solved in the body of the email
### Technical details:
- Redash Version: 0.9.2+b1536



Discussion:
I think there is a way to pass some message id header to prevent grouping. Will look into it.

I've given a quick look at the source, and I think that another quick fix would be to change the email subject define there: https://github.com/getredash/redash/blob/8b73a2b135aed8fcbca139380a959b9222ae0080/redash/destinations/email.py#L37 to be:
"{0} - {1}". What do you think about that ?


Resolving patch:
diff --git a/redash/destinations/email.py b/redash/destinations/email.py
--- a/redash/destinations/email.py
+++ b/redash/destinations/email.py
@@ -1,7 +1,7 @@
 import logging
 
 from flask_mail import Message
-from redash import models, mail
+from redash import mail, settings
 from redash.destinations import *
 
 
@@ -15,6 +15,11 @@ def configuration_schema(cls):
                 "addresses": {
                     "type": "string"
                 },
+                "subject_template": {
+                    "type": "string",
+                    "default": settings.ALERTS_DEFAULT_MAIL_SUBJECT_TEMPLATE,
+                    "title": "Subject Template"
+                }
             },
             "required": ["addresses"]
         }
@@ -36,9 +41,12 @@ def notify(self, alert, query, user, new_state, app, host, options):
 
         try:
             with app.app_context():
+                alert_name = alert.name.encode('utf-8', 'ignore')
+                state = new_state.upper()
+                subject_template = options.get('subject_template', settings.ALERTS_DEFAULT_MAIL_SUBJECT_TEMPLATE)
                 message = Message(
                     recipients=recipients,
-                    subject="[{1}] {0}".format(alert.name.encode('utf-8', 'ignore'), new_state.upper()),
+                    subject=subject_template.format(alert_name=alert_name, state=state),
                     html=html
                 )
                 mail.send(message)
diff --git a/redash/settings.py b/redash/settings.py
--- a/redash/settings.py
+++ b/redash/settings.py
@@ -141,6 +141,8 @@ def all_settings():
 
 HOST = os.environ.get('REDASH_HOST', '')
 
+ALERTS_DEFAULT_MAIL_SUBJECT_TEMPLATE = os.environ.get('REDASH_ALERTS_DEFAULT_MAIL_SUBJECT_TEMPLATE', "({state}) {alert_name}")
+
 # CORS settings for the Query Result API (and possbily future external APIs).
 # In most cases all you need to do is set REDASH_CORS_ACCESS_CONTROL_ALLOW_ORIGIN
 # to the calling domain (or domains in a comma separated list).


Test patch: