diff --git a/tests/test_email.py b/tests/test_email.py index db319f81f81cd28eaa834c3630f01b7461e14525..12a6534ed4c165e42ba79f838a11d9a9b32afbd6 100755 --- a/tests/test_email.py +++ b/tests/test_email.py @@ -20,7 +20,7 @@ except ImportError: import spf # install netstat if not present -if subprocess.call(["which", "netstat"]) != 0: +if subprocess.call(["which", "netstat"], stdout=subprocess.DEVNULL) != 0: subprocess.check_call(["apt-get", "-qq", "-y", "install", "net-tools"]) sys.path.append(str(Path(__file__).parents[1].resolve())) @@ -66,7 +66,7 @@ def check_relay(conf: dict) -> int: if status == 0: configured_relay = ( - out[len("relayhost") :].strip(" \t=").replace("[", "").replace("]", "") + out[len("relayhost"):].strip(" \t=").replace("[", "").replace("]", "") ) else: configured_relay = "" @@ -102,7 +102,7 @@ def check_relay(conf: dict) -> int: return 0 -def check_send_test_email() -> int: +def check_send_test_email(conf: dict) -> int: """Check that Postfix can send email. :return: Exit return code @@ -111,10 +111,23 @@ def check_send_test_email() -> int: print("Checking Postfix can send email:") + # check if s-nail is installed, if not set from address + sender = "" + status, out = subprocess.getstatusoutput("dpkg -l s-nail | grep -E '^ii'") + if status == 0: + u.warning("The package 's-nail' is installed, the email sender address will be ignored.") + else: + sender = conf.get("EMAIL_SENDER") or "" + # send email email = "noreply+{}-{}@ubicast.eu".format(time.time(), random.randint(0, 1000)) u.info("Sending test email to '{}'".format(email)) - cmd = "echo 'test email' | mail -s 'test email' {}".format(email) + if sender: + u.info("Sender address is '{}'".format(sender)) + sender = "-a 'From: {}' ".format(sender) + else: + u.info("Sender address is not set") + cmd = "echo 'test email' | mail -s 'Email used to test configuration.' {}{}".format(sender, email) subprocess.getstatusoutput(cmd) # init vars @@ -182,7 +195,7 @@ def main(): rcode = check_listen() if rcode == 0: rcode = check_relay(conf) - if check_send_test_email() == 1: + if check_send_test_email(conf) == 1: rcode = 1 sys.exit(rcode)