comparison sat/tools/common/email.py @ 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 559a625a236b
children be6d91572633
comparison
equal deleted inserted replaced
3464:54b9cdbeb335 3465:1c7f4ce6a4a2
22 22
23 from email.mime.text import MIMEText 23 from email.mime.text import MIMEText
24 from twisted.mail import smtp 24 from twisted.mail import smtp
25 from sat.core.constants import Const as C 25 from sat.core.constants import Const as C
26 from sat.tools import config as tools_config 26 from sat.tools import config as tools_config
27 from sat.core.log import getLogger
28
29 log = getLogger(__name__)
27 30
28 31
29 def sendEmail(config, to_emails, subject="", body="", from_email=None): 32 def sendEmail(config, to_emails, subject="", body="", from_email=None):
30 """Send an email using SàT configuration 33 """Send an email using SàT configuration
31 34
44 if email_from is None: 47 if email_from is None:
45 # we suppose that email domain and XMPP domain are identical 48 # we suppose that email domain and XMPP domain are identical
46 domain = tools_config.getConfig(config, None, "xmpp_domain", "example.net") 49 domain = tools_config.getConfig(config, None, "xmpp_domain", "example.net")
47 email_from = "no_reply@" + domain 50 email_from = "no_reply@" + domain
48 email_sender_domain = tools_config.getConfig(config, None, "email_sender_domain") 51 email_sender_domain = tools_config.getConfig(config, None, "email_sender_domain")
52 if email_sender_domain is None and smtp.DNSNAME == b"localhost.localdomain":
53 try:
54 email_sender_domain = email_from.split('@')[1]
55 except IndexError:
56 log.warning(
57 "Can't set email_sender_domain domain du to invalid email_from: "
58 f"{email_from!r}")
49 email_port = int(tools_config.getConfig(config, None, "email_port", 25)) 59 email_port = int(tools_config.getConfig(config, None, "email_port", 25))
50 email_username = tools_config.getConfig(config, None, "email_username") 60 email_username = tools_config.getConfig(config, None, "email_username")
51 email_password = tools_config.getConfig(config, None, "email_password") 61 email_password = tools_config.getConfig(config, None, "email_password")
52 email_auth = C.bool(tools_config.getConfig(config, None, "email_auth", C.BOOL_FALSE)) 62 email_auth = C.bool(tools_config.getConfig(config, None, "email_auth", C.BOOL_FALSE))
53 email_starttls = C.bool(tools_config.getConfig(config, None, "email_starttls", 63 email_starttls = C.bool(tools_config.getConfig(config, None, "email_starttls",
61 return smtp.sendmail( 71 return smtp.sendmail(
62 email_host.encode("utf-8"), 72 email_host.encode("utf-8"),
63 email_from.encode("utf-8"), 73 email_from.encode("utf-8"),
64 [email.encode("utf-8") for email in to_emails], 74 [email.encode("utf-8") for email in to_emails],
65 msg.as_bytes(), 75 msg.as_bytes(),
66 senderDomainName=email_sender_domain.encode("utf-8") if email_sender_domain 76 senderDomainName=email_sender_domain if email_sender_domain else None,
67 else None,
68 port=email_port, 77 port=email_port,
69 username=email_username.encode("utf-8") if email_username else None, 78 username=email_username.encode("utf-8") if email_username else None,
70 password=email_password.encode("utf-8") if email_password else None, 79 password=email_password.encode("utf-8") if email_password else None,
71 requireAuthentication=email_auth, 80 requireAuthentication=email_auth,
72 requireTransportSecurity=email_starttls, 81 requireTransportSecurity=email_starttls,