diff --git a/law.cfg.example b/law.cfg.example index 403f2f06..7af205f7 100644 --- a/law.cfg.example +++ b/law.cfg.example @@ -986,13 +986,13 @@ ; mail_smtp_host ; Description: The SMTP host for notifications sent by "law.notification.notify_mail". -; Type: ``str`` -; Default: ``"127.0.0.1"`` +; Type: ``str``, ``None`` +; Default: ``None`` ; mail_smtp_port ; Description: The SMTP port for notifications sent by "law.notification.notify_mail". -; Type: ``int`` -; Default: ``25`` +; Type: ``int``, ``None`` +; Default: ``None`` ; --- Options of contrib packages diff --git a/law/config.py b/law/config.py index a0176290..f215e125 100644 --- a/law/config.py +++ b/law/config.py @@ -138,8 +138,8 @@ def __str__(self) -> str: "notifications": { "mail_recipient": None, "mail_sender": None, - "mail_smtp_host": "127.0.0.1", - "mail_smtp_port": 25, + "mail_smtp_host": None, + "mail_smtp_port": None, }, "bash_sandbox": { "stagein_dir_name": "stagein", diff --git a/law/notification.py b/law/notification.py index 133fee96..e2f7f62a 100644 --- a/law/notification.py +++ b/law/notification.py @@ -33,21 +33,26 @@ def notify_mail( """ cfg = Config.instance() + # get config recipient and sender values from config if not recipient: recipient = cfg.get_expanded("notifications", "mail_recipient") if not sender: sender = cfg.get_expanded("notifications", "mail_sender") - if not smtp_host: - smtp_host = cfg.get_expanded("notifications", "mail_smtp_host") - if not smtp_port: - smtp_port = cfg.get_expanded("notifications", "mail_smtp_port") - if not recipient or not sender: logger.warning( f"cannot send mail notification, recipient ({recipient}) or sender ({sender}) empty", ) return False + # get host and port + if not smtp_port: + smtp_port = cfg.get_expanded("notifications", "mail_smtp_port") + if not smtp_host: + smtp_host = cfg.get_expanded("notifications", "mail_smtp_host") + # infer host from sender when not set + if not smtp_host: + smtp_host = sender.split("@", 1)[1] + mail_kwargs: dict[str, str | int] = {} if smtp_host: mail_kwargs["smtp_host"] = smtp_host diff --git a/law/util.py b/law/util.py index 4eefd726..6a5145ea 100644 --- a/law/util.py +++ b/law/util.py @@ -2124,7 +2124,7 @@ def send_mail( try: server = smtplib.SMTP(smtp_host, smtp_port) except Exception as e: - logger.warning(f"cannot create SMTP server: {e}") + logger.warning(f"cannot create SMTP server {smtp_host}:{smtp_port}: {e}") return False header = f"From: {sender}\r\nTo: {recipient}\r\nSubject: {subject}\r\n\r\n"