From 32899b71e26730d4a9e9440521b8ada50f223f9e Mon Sep 17 00:00:00 2001 From: Goffi Date: Fri, 19 Feb 2021 15:53:24 +0100 Subject: [PATCH] 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`. --- sat/tools/common/email.py | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/sat/tools/common/email.py b/sat/tools/common/email.py index 159fe013..00a2e07b 100644 --- a/sat/tools/common/email.py +++ b/sat/tools/common/email.py @@ -24,6 +24,9 @@ from email.mime.text import MIMEText 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 @@ def sendEmail(config, to_emails, subject="", body="", from_email=None): 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 @@ def sendEmail(config, to_emails, subject="", body="", from_email=None): 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,