fix: smtp check crash

In a container, the smtp check would fail,
even if SMTP_MUST_BE_CONNECTED was false,
because no address was bindable
This commit is contained in:
Yannik Schmidt
2023-01-04 04:06:55 +01:00
parent cc8bd685d0
commit 4dd7c77ce9

View File

@@ -5,6 +5,10 @@ import email.message
def checkSMTPConnection(app): def checkSMTPConnection(app):
'''Check connection and login on SMTP server configured in app config''' '''Check connection and login on SMTP server configured in app config'''
if not app.config["SMTP_MUST_BE_CONNECTED"]:
print("Warning: SMTP unusable: {}".format(e), file=sys.stderr)
return
try: try:
if not all([ x in app.config and app.config[x] for x in ["TARGET_SMTP", "TARGET_EMAIL"]]): if not all([ x in app.config and app.config[x] for x in ["TARGET_SMTP", "TARGET_EMAIL"]]):
raise smtplib.SMTPException("Missing Configuration.") raise smtplib.SMTPException("Missing Configuration.")
@@ -13,11 +17,8 @@ def checkSMTPConnection(app):
smtp.login(app.config["TARGET_SMTP_USER"], app.config["TARGET_SMTP_PASSWORD"]) smtp.login(app.config["TARGET_SMTP_USER"], app.config["TARGET_SMTP_PASSWORD"])
smtpTarget.quit() smtpTarget.quit()
except (smtplib.SMTPException, ConnectionRefusedError) as e: except (smtplib.SMTPException, ConnectionRefusedError) as e:
if app.config["SMTP_MUST_BE_CONNECTED"]:
print(e, file=sys.stderr) print(e, file=sys.stderr)
sys.exit(1) sys.exit(1)
else:
print("Warning: SMTP unusable: {}".format(e), file=sys.stderr)
def sendMailFromHtmlForm(app, htmlForm): def sendMailFromHtmlForm(app, htmlForm):
'''Take the app config and the contact HTML-form and send a mail accordingly''' '''Take the app config and the contact HTML-form and send a mail accordingly'''