From 48ae859e1bfb772de7c218b56c7bfdb467bc6719 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adri=C3=A0=20Casaj=C3=BAs?= Date: Mon, 6 Feb 2023 16:53:10 +0100 Subject: [PATCH] Fix: Set the smtp default port in config to allow connect to port 25 with TLS (#1564) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Adrià Casajús --- app/config.py | 6 +++++- app/mail_sender.py | 10 ++++------ 2 files changed, 9 insertions(+), 7 deletions(-) diff --git a/app/config.py b/app/config.py index 775ca505..5fdb7ab3 100644 --- a/app/config.py +++ b/app/config.py @@ -111,11 +111,15 @@ POSTFIX_SERVER = os.environ.get("POSTFIX_SERVER", "240.0.0.1") DISABLE_REGISTRATION = "DISABLE_REGISTRATION" in os.environ # allow using a different postfix port, useful when developing locally -POSTFIX_PORT = int(os.environ.get("POSTFIX_PORT", 25)) # Use port 587 instead of 25 when sending emails through Postfix # Useful when calling Postfix from an external network POSTFIX_SUBMISSION_TLS = "POSTFIX_SUBMISSION_TLS" in os.environ +if POSTFIX_SUBMISSION_TLS: + default_postfix_port = 587 +else: + default_postfix_port = 25 +POSTFIX_PORT = int(os.environ.get("POSTFIX_PORT", default_postfix_port)) POSTFIX_TIMEOUT = os.environ.get("POSTFIX_TIMEOUT", 3) # ["domain1.com", "domain2.com"] diff --git a/app/mail_sender.py b/app/mail_sender.py index a6737ed1..2705dddc 100644 --- a/app/mail_sender.py +++ b/app/mail_sender.py @@ -117,14 +117,12 @@ class MailSender: return True def _send_to_smtp(self, send_request: SendRequest, retries: int) -> bool: - if config.POSTFIX_SUBMISSION_TLS and config.POSTFIX_PORT == 25: - smtp_port = 587 - else: - smtp_port = config.POSTFIX_PORT try: start = time.time() with SMTP( - config.POSTFIX_SERVER, smtp_port, timeout=config.POSTFIX_TIMEOUT + config.POSTFIX_SERVER, + config.POSTFIX_PORT, + timeout=config.POSTFIX_TIMEOUT, ) as smtp: if config.POSTFIX_SUBMISSION_TLS: smtp.starttls() @@ -170,7 +168,7 @@ class MailSender: LOG.e(f"Ignore smtp error {e}") return False LOG.e( - f"Could not send message to smtp server {config.POSTFIX_SERVER}:{smtp_port}" + f"Could not send message to smtp server {config.POSTFIX_SERVER}:{config.POSTFIX_PORT}" ) self._save_request_to_unsent_dir(send_request) return False