summary refs log tree commit diff
path: root/synapse/push
diff options
context:
space:
mode:
authorDavid Baker <dave@matrix.org>2016-04-20 18:35:29 +0100
committerDavid Baker <dave@matrix.org>2016-04-20 18:35:29 +0100
commit2ed0adb075b745e6586ca88ce7cf6b169460a7d7 (patch)
tree1b4ba07bba23df1a85b975a8b20c6c080a5dff29 /synapse/push
parentmore pep8 (diff)
downloadsynapse-2ed0adb075b745e6586ca88ce7cf6b169460a7d7.tar.xz
Generate mails from a template
Diffstat (limited to 'synapse/push')
-rw-r--r--synapse/push/emailpusher.py12
-rw-r--r--synapse/push/mailer.py30
2 files changed, 21 insertions, 21 deletions
diff --git a/synapse/push/emailpusher.py b/synapse/push/emailpusher.py
index 820c8f8467..4e21221fb7 100644
--- a/synapse/push/emailpusher.py
+++ b/synapse/push/emailpusher.py
@@ -61,11 +61,7 @@ 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,
-            )
+            self.mailer = Mailer(self.hs)
         else:
             self.mailer = None
 
@@ -149,7 +145,7 @@ class EmailPusher(object):
                 # *one* email updating the user on their notifications,
                 # we then consider all previously outstanding notifications
                 # to be delivered.
-                yield self.send_notification(push_action)
+                yield self.send_notification(unprocessed)
 
                 yield self.save_last_stream_ordering_and_success(max([
                     ea['stream_ordering'] for ea in unprocessed
@@ -252,8 +248,8 @@ class EmailPusher(object):
         )
 
     @defer.inlineCallbacks
-    def send_notification(self, push_action):
+    def send_notification(self, push_actions):
         logger.info("Sending notif email for user %r", self.user_id)
         yield self.mailer.send_notification_mail(
-            self.user_id, self.email, push_action
+            self.user_id, self.email, push_actions
         )
diff --git a/synapse/push/mailer.py b/synapse/push/mailer.py
index 97cba2ec2b..0f20d43f75 100644
--- a/synapse/push/mailer.py
+++ b/synapse/push/mailer.py
@@ -15,34 +15,38 @@
 
 from twisted.internet import defer
 
-import smtplib
+from twisted.mail.smtp import sendmail
 import email.utils
 import email.mime.multipart
 from email.mime.text import MIMEText
 
+import jinja2
+
 
 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
+    def __init__(self, hs):
+        self.hs = hs
+        loader = jinja2.FileSystemLoader(self.hs.config.email_template_dir)
+        env = jinja2.Environment(loader=loader)
+        self.notif_template = env.get_template(self.hs.config.email_notif_template_html)
 
     @defer.inlineCallbacks
-    def send_notification_mail(self, user_id, email_address, push_action):
-        raw_from = email.utils.parseaddr(self.notif_from)[1]
+    def send_notification_mail(self, user_id, email_address, push_actions):
+        raw_from = email.utils.parseaddr(self.hs.config.email_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!"
+        plainText = self.notif_template.render()
 
         text_part = MIMEText(plainText, "plain")
         text_part['Subject'] = "New Matrix Notifications"
-        text_part['From'] = self.notif_from
+        text_part['From'] = self.hs.config.email_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()
+        yield sendmail(
+            self.hs.config.email_smtp_host,
+            raw_from, raw_to, text_part.as_string(),
+            port=self.hs.config.email_smtp_port
+        )