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`.
This commit is contained in:
Goffi 2021-02-19 15:53:24 +01:00
commit 32899b71e2

View file

@ -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,