Send a rather basic email notif

Also pep8 fixes
This commit is contained in:
David Baker 2016-04-20 13:02:01 +01:00
parent e2a01455af
commit f63bd4ff47
6 changed files with 136 additions and 12 deletions

View file

@ -0,0 +1,62 @@
# -*- coding: utf-8 -*-
# Copyright 2015, 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.
# This file can't be called email.py because if it is, we cannot:
import email.utils
from ._base import Config
class EmailConfig(Config):
"""
Email Configuration
"""
def read_config(self, config):
email_config = config.get("email", None)
if email_config:
self.email_enable_notifs = email_config.get("enable_notifs", True)
if (
"smtp_host" not in email_config or
"smtp_port" not in email_config or
"notif_from" not in email_config
):
raise RuntimeError(
"You must set smtp_host, smtp_port and notif_from "
"to send email notifications"
)
self.email_smtp_host = email_config["smtp_host"]
self.email_smtp_port = email_config["smtp_port"]
self.email_notif_from = email_config["notif_from"]
# make sure it's valid
parsed = email.utils.parseaddr(self.email_notif_from)
if parsed[1] == '':
raise RuntimeError("Invalid notif_from address")
else:
self.email_enable_notifs = False
self.email_smtp_host = None
self.email_smtp_port = None
self.email_notif_from = None
def default_config(self, config_dir_path, server_name, **kwargs):
return """
# Enable sending emails for notification events
#email_config:
# enable_notifs: false
# smtp_host: "localhost"
# smtp_port: 25
"""

View file

@ -31,13 +31,14 @@ from .cas import CasConfig
from .password import PasswordConfig
from .jwt import JWTConfig
from .ldap import LDAPConfig
from .emailconfig import EmailConfig
class HomeServerConfig(TlsConfig, ServerConfig, DatabaseConfig, LoggingConfig,
RatelimitConfig, ContentRepositoryConfig, CaptchaConfig,
VoipConfig, RegistrationConfig, MetricsConfig, ApiConfig,
AppServiceConfig, KeyConfig, SAML2Config, CasConfig,
JWTConfig, LDAPConfig, PasswordConfig,):
JWTConfig, LDAPConfig, PasswordConfig, EmailConfig,):
pass

View file

@ -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)
logger.info("Sending notif email for user %r", self.user_id)
yield self.mailer.send_notification_mail(
self.user_id, self.email, push_action
)

48
synapse/push/mailer.py Normal file
View file

@ -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()

View file

@ -202,7 +202,6 @@ class EventPushActionsStore(SQLBaseStore):
result = yield self.runInteraction("get_time_of_last_push_action_before", f)
defer.returnValue(result[0] if result is not None else None)
@defer.inlineCallbacks
def get_latest_push_action_stream_ordering(self):
def f(txn):