Browse Source

Send email to invited users

Daniel Wagner-Hall 8 years ago
parent
commit
9d300cc28a

+ 6 - 0
sydent/http/servlets/store_invite_servlet.py

@@ -26,6 +26,7 @@ from sydent.db.invite_tokens import JoinTokenStore
 from sydent.db.threepid_associations import GlobalAssociationStore
 
 from sydent.http.servlets import require_args, send_cors
+from sydent.util.emailutils import sendEmail
 
 
 class StoreInviteServlet(Resource):
@@ -63,6 +64,11 @@ class StoreInviteServlet(Resource):
 
         JoinTokenStore(self.sydent).storeToken(medium, address, roomId, sender, token)
 
+        sendEmail(self.sydent, "email.invite_template", address, {
+            "room_id": roomId,
+            "sender": sender,
+        })
+
         pubKey = self.sydent.keyring.ed25519.verify_key
         pubKeyBase64 = encode_base64(pubKey.encode())
 

+ 60 - 0
sydent/util/emailutils.py

@@ -0,0 +1,60 @@
+# -*- coding: utf-8 -*-
+
+# Copyright 2014-2015 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.
+
+import logging
+import smtplib
+import email.utils
+import twisted.python.log
+
+import email.utils
+
+logger = logging.getLogger(__name__)
+
+
+def sendEmail(sydent, templateName, mailTo, substitutions):
+        mailFrom = sydent.cfg.get('email', 'email.from')
+        mailTemplateFile = sydent.cfg.get('email', templateName)
+        allSubstitutions = {
+            'date': email.utils.formatdate(localtime=False),
+            'to': mailTo,
+            'from': mailFrom,
+        }
+        allSubstitutions.update(substitutions)
+        mailString = open(mailTemplateFile).read() % allSubstitutions
+        rawFrom = email.utils.parseaddr(mailFrom)[1]
+        rawTo = email.utils.parseaddr(mailTo)[1]
+        if rawFrom == '' or rawTo == '':
+            logger.info("Couldn't parse from / to address %s / %s", mailFrom, mailTo)
+            raise EmailAddressException()
+        mailServer = sydent.cfg.get('email', 'email.smtphost')
+        logger.info("Sending mail to %s with mail server: %s" % (mailTo, mailServer,))
+        try:
+            smtp = smtplib.SMTP(mailServer)
+            smtp.sendmail(rawFrom, rawTo, mailString)
+            smtp.quit()
+        except Exception as origException:
+            twisted.python.log.err()
+            ese = EmailSendException()
+            ese.cause = origException
+            raise ese
+
+
+class EmailAddressException(Exception):
+    pass
+
+
+class EmailSendException(Exception):
+    pass

+ 5 - 45
sydent/validators/emailvalidator.py

@@ -14,18 +14,14 @@
 # See the License for the specific language governing permissions and
 # limitations under the License.
 
-import smtplib
 import os
-import email.utils
 import logging
 import random
 import string
 import urllib
-import twisted.python.log
-
-import email.utils
 
 from sydent.db.valsession import ThreePidValSessionStore
+from sydent.util.emailutils import sendEmail
 from sydent.validators import ValidationSession
 
 from sydent.util import time_msec
@@ -56,45 +52,17 @@ class EmailValidator:
         messageid = "%d%s@%s" % (time_msec(), midRandom, myHostname)
         ipstring = ipaddress if ipaddress else u"an unknown location"
 
-        mailTo = emailAddress
-        mailFrom = self.sydent.cfg.get('email', 'email.from')
-
-        mailTemplateFile = self.sydent.cfg.get('email', 'email.template')
-
-        mailString = open(mailTemplateFile).read() % {
-            'date': email.utils.formatdate(localtime=False),
-            'to': mailTo,
-            'from': mailFrom,
+        substitutions = {
             'messageid': messageid,
             'ipaddress': ipstring,
             'link': self.makeValidateLink(valSession, clientSecret, nextLink),
             'token': valSession.token,
         }
-
-        rawFrom = email.utils.parseaddr(mailFrom)[1]
-        rawTo = email.utils.parseaddr(mailTo)[1]
-
-        if rawFrom == '' or rawTo == '':
-            logger.info("Couldn't parse from / to address %s / %s", mailFrom, mailTo)
-            raise EmailAddressException()
-
-        mailServer = self.sydent.cfg.get('email', 'email.smtphost')
-
         logger.info(
-            "Attempting to mail code %s (nextLink: %s)"
-            " to %s using mail server %s",
-            valSession.token, nextLink, rawTo, mailServer
+            "Attempting to mail code %s (nextLink: %s) to %s",
+            valSession.token, nextLink, emailAddress,
         )
-
-        try:
-            smtp = smtplib.SMTP(mailServer)
-            smtp.sendmail(rawFrom, rawTo, mailString)
-            smtp.quit()
-        except Exception as origException:
-            twisted.python.log.err()
-            ese = EmailSendException()
-            ese.cause = origException
-            raise ese
+        sendEmail(self.sydent, 'email.template', emailAddress, substitutions)
 
         valSessionStore.setSendAttemptNumber(valSession.id, sendAttempt)
 
@@ -148,11 +116,3 @@ class EmailValidator:
         else:
             logger.info("Incorrect token submitted")
             return False
-
-
-class EmailAddressException(Exception):
-    pass
-
-
-class EmailSendException(Exception):
-    pass