diff --git a/imageroot/actions/add-relay-rule/10test-credentials b/imageroot/actions/add-relay-rule/10test-credentials index d9314855..b184c12c 100755 --- a/imageroot/actions/add-relay-rule/10test-credentials +++ b/imageroot/actions/add-relay-rule/10test-credentials @@ -23,10 +23,6 @@ password = data['password'] rule_type = data['rule_type'] rule_subject = data['rule_subject'] -# if the username is empty, we do not need to validate -if not username: - sys.exit(0) - def get_password_from_database(rule_subject): # Function to query the database for the password based rule_subject sdb = mail.pcdb_connect() @@ -43,7 +39,7 @@ def get_password_from_database(rule_subject): sys.exit(6) # password not changed, we need to retrieve it from the database -if password == '': +if username and password == '': password = get_password_from_database(rule_subject) port = int(data['port']) @@ -57,13 +53,18 @@ ctx.verify_mode = ssl.CERT_NONE try : if tls: - smtp = smtplib.SMTP(host, port=port, timeout=10) - smtp.starttls(context=ctx) + # first we try to connect with starttls if it fails we try with smtps + try: + smtp = smtplib.SMTP(host, port=port, timeout=10) + smtp.starttls(context=ctx) + except Exception as ex: + print(agent.SD_WARNING+"STARTTLS seems not supported. Fall back to SMTPS", ex, file=sys.stderr) + smtp = smtplib.SMTP_SSL(host, port=port, timeout=10, context=ctx) else: smtp = smtplib.SMTP(host, port=port, timeout=10) # we have a login, we try to authenticate. - if username: + if username and password: smtp.login(username, password) # without authentication, we have now way to test except # to connect to the server. we quit