Fix minor issues with email config (#6962)

* Give `notif_template_html`, `notif_template_text` default values (fixes #6960)
 * Don't complain if `smtp_host` and `smtp_port` are unset, since they have sensible defaults (fixes #6961)
 * Set the example for `enable_notifs` to `True`, for consistency and because it's more useful
 * Raise errors as ConfigError rather than RuntimeError for nicer formatting
This commit is contained in:
Richard van der Hoff 2020-02-24 15:18:38 +00:00 committed by GitHub
parent af6c389501
commit 4c2ed3f20e
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 36 additions and 40 deletions

1
changelog.d/6962.bugfix Normal file
View file

@ -0,0 +1 @@
Fix a couple of bugs in email configuration handling.

View file

@ -1409,10 +1409,6 @@ email:
# #
#require_transport_security: true #require_transport_security: true
# Enable sending emails for messages that the user has missed
#
#enable_notifs: false
# notif_from defines the "From" address to use when sending emails. # notif_from defines the "From" address to use when sending emails.
# It must be set if email sending is enabled. # It must be set if email sending is enabled.
# #
@ -1430,6 +1426,11 @@ email:
# #
#app_name: my_branded_matrix_server #app_name: my_branded_matrix_server
# Uncomment the following to enable sending emails for messages that the user
# has missed. Disabled by default.
#
#enable_notifs: true
# Uncomment the following to disable automatic subscription to email # Uncomment the following to disable automatic subscription to email
# notifications for new users. Enabled by default. # notifications for new users. Enabled by default.
# #

View file

@ -27,6 +27,12 @@ import pkg_resources
from ._base import Config, ConfigError from ._base import Config, ConfigError
MISSING_PASSWORD_RESET_CONFIG_ERROR = """\
Password reset emails are enabled on this homeserver due to a partial
'email' block. However, the following required keys are missing:
%s
"""
class EmailConfig(Config): class EmailConfig(Config):
section = "email" section = "email"
@ -142,24 +148,18 @@ class EmailConfig(Config):
bleach bleach
if self.threepid_behaviour_email == ThreepidBehaviour.LOCAL: if self.threepid_behaviour_email == ThreepidBehaviour.LOCAL:
required = ["smtp_host", "smtp_port", "notif_from"]
missing = [] missing = []
for k in required: if not self.email_notif_from:
if k not in email_config: missing.append("email.notif_from")
missing.append("email." + k)
# public_baseurl is required to build password reset and validation links that # public_baseurl is required to build password reset and validation links that
# will be emailed to users # will be emailed to users
if config.get("public_baseurl") is None: if config.get("public_baseurl") is None:
missing.append("public_baseurl") missing.append("public_baseurl")
if len(missing) > 0: if missing:
raise RuntimeError( raise ConfigError(
"Password resets emails are configured to be sent from " MISSING_PASSWORD_RESET_CONFIG_ERROR % (", ".join(missing),)
"this homeserver due to a partial 'email' block. "
"However, the following required keys are missing: %s"
% (", ".join(missing),)
) )
# These email templates have placeholders in them, and thus must be # These email templates have placeholders in them, and thus must be
@ -245,32 +245,25 @@ class EmailConfig(Config):
) )
if self.email_enable_notifs: if self.email_enable_notifs:
required = [
"smtp_host",
"smtp_port",
"notif_from",
"notif_template_html",
"notif_template_text",
]
missing = [] missing = []
for k in required: if not self.email_notif_from:
if k not in email_config: missing.append("email.notif_from")
missing.append(k)
if len(missing) > 0:
raise RuntimeError(
"email.enable_notifs is True but required keys are missing: %s"
% (", ".join(["email." + k for k in missing]),)
)
if config.get("public_baseurl") is None: if config.get("public_baseurl") is None:
raise RuntimeError( missing.append("public_baseurl")
"email.enable_notifs is True but no public_baseurl is set"
if missing:
raise ConfigError(
"email.enable_notifs is True but required keys are missing: %s"
% (", ".join(missing),)
) )
self.email_notif_template_html = email_config["notif_template_html"] self.email_notif_template_html = email_config.get(
self.email_notif_template_text = email_config["notif_template_text"] "notif_template_html", "notif_mail.html"
)
self.email_notif_template_text = email_config.get(
"notif_template_text", "notif_mail.txt"
)
for f in self.email_notif_template_text, self.email_notif_template_html: for f in self.email_notif_template_text, self.email_notif_template_html:
p = os.path.join(self.email_template_dir, f) p = os.path.join(self.email_template_dir, f)
@ -323,10 +316,6 @@ class EmailConfig(Config):
# #
#require_transport_security: true #require_transport_security: true
# Enable sending emails for messages that the user has missed
#
#enable_notifs: false
# notif_from defines the "From" address to use when sending emails. # notif_from defines the "From" address to use when sending emails.
# It must be set if email sending is enabled. # It must be set if email sending is enabled.
# #
@ -344,6 +333,11 @@ class EmailConfig(Config):
# #
#app_name: my_branded_matrix_server #app_name: my_branded_matrix_server
# Uncomment the following to enable sending emails for messages that the user
# has missed. Disabled by default.
#
#enable_notifs: true
# Uncomment the following to disable automatic subscription to email # Uncomment the following to disable automatic subscription to email
# notifications for new users. Enabled by default. # notifications for new users. Enabled by default.
# #