# HG changeset patch # User Goffi # Date 1613746404 -3600 # Node ID 1c7f4ce6a4a2e7c2418b42c0b63509ebd37fd4e4 # Parent 54b9cdbeb335d02dfb538d4c3e15fb11625c7381 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`. diff -r 54b9cdbeb335 -r 1c7f4ce6a4a2 sat/tools/common/email.py --- 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,