From 5d4081eca12a72826fd4f35bd8f7e926d3c9665e Mon Sep 17 00:00:00 2001
From: Nicolas KAROLAK <nicolas@karolak.fr>
Date: Tue, 7 May 2019 13:42:18 +0200
Subject: [PATCH] handle EMAIL_SMTP_SERVER host:port notation

---
 tests/test_email.py | 23 +++++++++++------------
 1 file changed, 11 insertions(+), 12 deletions(-)

diff --git a/tests/test_email.py b/tests/test_email.py
index 97284414..7a2308d7 100755
--- a/tests/test_email.py
+++ b/tests/test_email.py
@@ -43,11 +43,13 @@ def check_listen() -> tuple:
     return warnings, errors
 
 
-def check_relay(relay: str, domain: str) -> tuple:
+def check_relay(relay_host: str, relay_port: str, domain: str) -> tuple:
     """Check that Postfix is not an open relay.
 
-    :param relay: Hostname or IP address of relay host
-    :type relay: str
+    :param relay_host: Hostname or IP address of relay host
+    :type relay_host: str
+    :param relay_port: Port of relay host
+    :type relay_port: str
     :param domain: Domain name under which mails will be send
     :type domain: str
     :return: Exit return codes
@@ -83,6 +85,7 @@ def check_relay(relay: str, domain: str) -> tuple:
             u.warning('"myorigin" setting does not contain a valid domain')
             warnings += 1
 
+    relay = "{}:{}".format(relay_host, relay_port) if relay_port else relay_host
     if relay != configured_relay:
         u.error("STMP relay must be {}".format(relay))
         errors += 1
@@ -110,8 +113,7 @@ def check_send(sender: str) -> tuple:
     if sender:
         sender = "-a 'From: {}' ".format(sender)
     else:
-        u.warning("Sender address is not set")
-        warnings += 1
+        u.info("Sender address is not set")
     cmd = "echo 'test email' | mail -s 'Email used to test configuration.' {}{}".format(
         sender, email
     )
@@ -210,12 +212,9 @@ def main():
 
     # get settings
     conf = u.load_conf()
-    relay_host = (
-        conf.get("EMAIL_SMTP_SERVER", "")
-        .replace("[", "")
-        .replace("]", "")
-        .split(":")[0]
-    )
+    relay = conf.get("EMAIL_SMTP_SERVER", "").replace("[", "").replace("]", "")
+    relay_host = relay.split(":")[0] if ":" in relay else relay
+    relay_port = relay.split(":")[-1] if ":" in relay else ""
     ip_addr = (
         (socket.gethostbyname(relay_host) if relay_host else None)
         or conf.get("NETWORK_IP_NAT")
@@ -235,7 +234,7 @@ def main():
     errors += check_err if check_err else errors
 
     # check that relayhost is correct
-    check_warn, check_err = check_relay(relay_host, domain)
+    check_warn, check_err = check_relay(relay_host, relay_port, domain)
     warnings += check_warn if check_warn else warnings
     errors += check_err if check_err else errors
 
-- 
GitLab