Skip to content
This repository has been archived by the owner on Apr 26, 2024. It is now read-only.

Fix minor issues with email config #6962

Merged
merged 4 commits into from
Feb 24, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions changelog.d/6962.bugfix
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fix a couple of bugs in email configuration handling.
9 changes: 5 additions & 4 deletions docs/sample_config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -1409,10 +1409,6 @@ email:
#
#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.
# It must be set if email sending is enabled.
#
Expand All @@ -1430,6 +1426,11 @@ email:
#
#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
# notifications for new users. Enabled by default.
#
Expand Down
66 changes: 30 additions & 36 deletions synapse/config/emailconfig.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,12 @@

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):
section = "email"
Expand Down Expand Up @@ -142,24 +148,18 @@ def read_config(self, config, **kwargs):
bleach

if self.threepid_behaviour_email == ThreepidBehaviour.LOCAL:
required = ["smtp_host", "smtp_port", "notif_from"]

missing = []
for k in required:
if k not in email_config:
missing.append("email." + k)
if not self.email_notif_from:
missing.append("email.notif_from")

# public_baseurl is required to build password reset and validation links that
# will be emailed to users
if config.get("public_baseurl") is None:
missing.append("public_baseurl")

if len(missing) > 0:
raise RuntimeError(
"Password resets emails are configured to be sent from "
"this homeserver due to a partial 'email' block. "
"However, the following required keys are missing: %s"
% (", ".join(missing),)
if missing:
raise ConfigError(
MISSING_PASSWORD_RESET_CONFIG_ERROR % (", ".join(missing),)
)

# These email templates have placeholders in them, and thus must be
Expand Down Expand Up @@ -245,32 +245,25 @@ def read_config(self, config, **kwargs):
)

if self.email_enable_notifs:
required = [
"smtp_host",
"smtp_port",
"notif_from",
"notif_template_html",
"notif_template_text",
]

missing = []
for k in required:
if k not in email_config:
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 not self.email_notif_from:
missing.append("email.notif_from")

if config.get("public_baseurl") is None:
raise RuntimeError(
"email.enable_notifs is True but no public_baseurl is set"
missing.append("public_baseurl")

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_text = email_config["notif_template_text"]
self.email_notif_template_html = email_config.get(
"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:
p = os.path.join(self.email_template_dir, f)
Expand Down Expand Up @@ -323,10 +316,6 @@ def generate_config_section(self, config_dir_path, server_name, **kwargs):
#
#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.
# It must be set if email sending is enabled.
#
Expand All @@ -344,6 +333,11 @@ def generate_config_section(self, config_dir_path, server_name, **kwargs):
#
#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
# notifications for new users. Enabled by default.
#
Expand Down