Repository: getredash/redash

Issue:
Email notification not working
Hi Arik,

thank you for re:dash, it is really amazing.
### Issue Summary

Redash 0.11.0+b1995 (and similar versions including 0.12RC I guess)
### Steps to Reproduce
1. Set an alert on a query that will trigger
2. Register to the alert with your user (email)
3. Refresh the query
### Technical details:

From the logs I get 
`celery_error.log:[2016-06-26 13:30:46,312: WARNING/Worker-11] redash.tasks.check_alerts_for_query[5bb71f82-ec70-4893-bb12-5d665f7f9a05]: Exception: 'str' object has no attribute 'get'`

I'm not really sure (*) but I think it is bubbling up in [redash/tasks/alerts.py:43](https://github.com/getredash/redash/blob/master/redash/tasks/alerts.py#L43)
from [redash/destinations/email.py:27](https://github.com/getredash/redash/blob/8b73a2b135aed8fcbca139380a959b9222ae0080/redash/destinations/email.py#L27)

(*) ConfigurationContainer may be doing something more than merely passing the dict along but I'm not sure ...

The root cause may be in [redash/models.py:1192](https://github.com/getredash/redash/blob/master/redash/models.py#L1192)
where it could be solved by:
`config = {'addresses': self.user.email}`

Hopefully this evening I will have a pull request.



Discussion:
You analyzed it correctly and I will push the fix later today myself (already have the fix, just didn't have the chance to make a pull request).


Resolving patch:
diff --git a/redash/destinations/email.py b/redash/destinations/email.py
--- a/redash/destinations/email.py
+++ b/redash/destinations/email.py
@@ -24,7 +24,11 @@ def icon(cls):
         return 'fa-envelope'
 
     def notify(self, alert, query, user, new_state, app, host, options):
-        recipients = [email for email in options.get('addresses').split(',') if email]
+        recipients = [email for email in options.get('addresses', '').split(',') if email]
+
+        if not recipients:
+            logging.warning("No emails given. Skipping send.")
+
         html = """
         Check <a href="{host}/alerts/{alert_id}">alert</a> / check <a href="{host}/queries/{query_id}">query</a>.
         """.format(host=host, alert_id=alert.id, query_id=query.id)
@@ -39,6 +43,6 @@ def notify(self, alert, query, user, new_state, app, host, options):
                 )
                 mail.send(message)
         except Exception:
-            logging.exception("mail send ERROR.")
+            logging.exception("Mail send error.")
 
 register(Email)
diff --git a/redash/models.py b/redash/models.py
--- a/redash/models.py
+++ b/redash/models.py
@@ -1189,12 +1189,11 @@ def notify(self, alert, query, user, new_state, app, host):
                                            app, host)
         else:
             # User email subscription, so create an email destination object
-            config = {'email': self.user.email}
+            config = {'addresses': self.user.email}
             schema = get_configuration_schema_for_destination_type('email')
-            options = ConfigurationContainer(json.dumps(config), schema)
+            options = ConfigurationContainer(config, schema)
             destination = get_destination('email', options)
-            return destination.notify(alert, query, user, new_state,
-                                           app, host, options)
+            return destination.notify(alert, query, user, new_state, app, host, options)
 
 
 all_models = (Organization, Group, DataSource, DataSourceGroup, User, QueryResult, Query, Alert, Dashboard, Visualization, Widget, Event, NotificationDestination, AlertSubscription, ApiKey)
diff --git a/redash/tasks/alerts.py b/redash/tasks/alerts.py
--- a/redash/tasks/alerts.py
+++ b/redash/tasks/alerts.py
@@ -42,5 +42,5 @@ def check_alerts_for_query(query_id):
                 try:
                     subscription.notify(alert, query, subscription.user, new_state, app, host)
                 except Exception as e:
-                    logger.warn("Exception: {}".format(e))
+                    logger.exception("Error with processing destination")
 


Test patch: