changeset 3465:1c7f4ce6a4a2

tools (common/email): try to guess sender domain when necessary: when `DNSNAME` is `localhost.localdomain` and is `mail_sender_domain` is not set, a guess is done to find a proper domain, using `email_from`.
author Goffi <goffi@goffi.org>
date Fri, 19 Feb 2021 15:53:24 +0100
parents 54b9cdbeb335
children 80f8635dc66e
files sat/tools/common/email.py
diffstat 1 files changed, 11 insertions(+), 2 deletions(-) [+]
line wrap: on
line diff
--- a/sat/tools/common/email.py	Fri Feb 19 15:53:20 2021 +0100
+++ b/sat/tools/common/email.py	Fri Feb 19 15:53:24 2021 +0100
@@ -24,6 +24,9 @@
 from twisted.mail import smtp
 from sat.core.constants import Const as C
 from sat.tools import config as tools_config
+from sat.core.log import getLogger
+
+log = getLogger(__name__)
 
 
 def sendEmail(config, to_emails, subject="", body="", from_email=None):
@@ -46,6 +49,13 @@
         domain = tools_config.getConfig(config, None, "xmpp_domain", "example.net")
         email_from = "no_reply@" + domain
     email_sender_domain = tools_config.getConfig(config, None, "email_sender_domain")
+    if email_sender_domain is None and smtp.DNSNAME == b"localhost.localdomain":
+        try:
+            email_sender_domain = email_from.split('@')[1]
+        except IndexError:
+            log.warning(
+                "Can't set email_sender_domain domain du to invalid email_from: "
+                f"{email_from!r}")
     email_port = int(tools_config.getConfig(config, None, "email_port", 25))
     email_username = tools_config.getConfig(config, None, "email_username")
     email_password = tools_config.getConfig(config, None, "email_password")
@@ -63,8 +73,7 @@
         email_from.encode("utf-8"),
         [email.encode("utf-8") for email in to_emails],
         msg.as_bytes(),
-        senderDomainName=email_sender_domain.encode("utf-8") if email_sender_domain
-                                                             else None,
+        senderDomainName=email_sender_domain if email_sender_domain else None,
         port=email_port,
         username=email_username.encode("utf-8") if email_username else None,
         password=email_password.encode("utf-8") if email_password else None,