summary refs log tree commit diff
path: root/synapse/push
diff options
context:
space:
mode:
authorDavid Baker <dave@matrix.org>2016-04-20 13:02:01 +0100
committerDavid Baker <dave@matrix.org>2016-04-20 13:02:01 +0100
commitf63bd4ff4704c9f7b6e23c76720dbd955a60c058 (patch)
treea503afc43fc4a6ab84190586f267a31c94cb466c /synapse/push
parentAdd single instance & logging stuff (diff)
downloadsynapse-f63bd4ff4704c9f7b6e23c76720dbd955a60c058.tar.xz
Send a rather basic email notif
Also pep8 fixes
Diffstat (limited to 'synapse/push')
-rw-r--r--synapse/push/emailpusher.py32
-rw-r--r--synapse/push/mailer.py48
2 files changed, 71 insertions, 9 deletions
diff --git a/synapse/push/emailpusher.py b/synapse/push/emailpusher.py
index 74e3a70562..820c8f8467 100644
--- a/synapse/push/emailpusher.py
+++ b/synapse/push/emailpusher.py
@@ -18,9 +18,10 @@ from twisted.internet import defer, reactor
 import logging
 
 from synapse.util.metrics import Measure
-from synapse.util.async import run_on_reactor
 from synapse.util.logcontext import LoggingContext
 
+from mailer import Mailer
+
 logger = logging.getLogger(__name__)
 
 # The amount of time we always wait before ever emailing about a notification
@@ -28,11 +29,11 @@ logger = logging.getLogger(__name__)
 DELAY_BEFORE_MAIL_MS = 2 * 60 * 1000
 
 THROTTLE_START_MS = 2 * 60 * 1000
-THROTTLE_MAX_MS = (2 * 60 * 1000) * (2**11)  # ~3 days
+THROTTLE_MAX_MS = (2 * 60 * 1000) * (2 ** 11)  # ~3 days
 
 # If no event triggers a notification for this long after the previous,
 # the throttle is released.
-THROTTLE_RESET_AFTER_MS = (2 * 60 * 1000) * (2**11)  # ~3 days
+THROTTLE_RESET_AFTER_MS = (2 * 60 * 1000) * (2 ** 11)  # ~3 days
 
 
 class EmailPusher(object):
@@ -59,12 +60,22 @@ class EmailPusher(object):
 
         self.processing = False
 
+        if self.hs.config.email_enable_notifs:
+            self.mailer = Mailer(
+                self.store,
+                self.hs.config.email_smtp_host, self.hs.config.email_smtp_port,
+                self.hs.config.email_notif_from,
+            )
+        else:
+            self.mailer = None
+
     @defer.inlineCallbacks
     def on_started(self):
-        self.throttle_params = yield self.store.get_throttle_params_by_room(
-            self.pusher_id
-        )
-        yield self._process()
+        if self.mailer is not None:
+            self.throttle_params = yield self.store.get_throttle_params_by_room(
+                self.pusher_id
+            )
+            yield self._process()
 
     def on_stop(self):
         if self.timed_call:
@@ -102,6 +113,7 @@ class EmailPusher(object):
                 finally:
                     self.processing = False
 
+    @defer.inlineCallbacks
     def _unsafe_process(self):
         """
         Main logic of the push loop without the wrapper function that sets
@@ -241,5 +253,7 @@ class EmailPusher(object):
 
     @defer.inlineCallbacks
     def send_notification(self, push_action):
-        yield run_on_reactor()
-        logger.error("sending notif email for user %r", self.user_id)
\ No newline at end of file
+        logger.info("Sending notif email for user %r", self.user_id)
+        yield self.mailer.send_notification_mail(
+            self.user_id, self.email, push_action
+        )
diff --git a/synapse/push/mailer.py b/synapse/push/mailer.py
new file mode 100644
index 0000000000..93d3866ec7
--- /dev/null
+++ b/synapse/push/mailer.py
@@ -0,0 +1,48 @@
+# -*- coding: utf-8 -*-
+# Copyright 2016 OpenMarket Ltd
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+#     http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+
+from twisted.internet import defer
+
+import smtplib
+import email.utils
+import email.mime.multipart
+from email.mime.text import MIMEText
+
+
+class Mailer(object):
+    def __init__(self, store, smtp_host, smtp_port, notif_from):
+        self.store = store
+        self.smtp_host = smtp_host
+        self.smtp_port = smtp_port
+        self.notif_from = notif_from
+
+    @defer.inlineCallbacks
+    def send_notification_mail(self, user_id, email_address, push_action):
+        raw_from = email.utils.parseaddr(self.notif_from)[1]
+        raw_to = email.utils.parseaddr(email_address)[1]
+
+        if raw_to == '':
+            raise RuntimeError("Invalid 'to' address")
+
+        plainText = "yo dawg, you got notifications!"
+
+        text_part = MIMEText(plainText, "plain")
+        text_part['Subject'] = "New Matrix Notifications"
+        text_part['From'] = self.notif_from
+        text_part['To'] = email_address
+
+        smtp = smtplib.SMTP(self.smtp_host, self.smtp_port)
+        smtp.sendmail(raw_from, raw_to, text_part.as_string())
+        smtp.quit()
\ No newline at end of file