Enable SSL SMTP

This commit is contained in:
Wang Wenlin 2023-02-28 08:53:47 +00:00 committed by Éloi Rivard
parent 9d3cd71164
commit 4fa09b3a70
2 changed files with 26 additions and 12 deletions

View file

@ -194,6 +194,7 @@ WEBSITE = "{{ user.labeledURI[0] }}"
# HOST = "localhost"
# PORT = 25
# TLS = false
# SSL = false
# LOGIN = ""
# PASSWORD = ""
# FROM_ADDR = "admin@mydomain.tld"

View file

@ -17,6 +17,7 @@ from .apputils import profile_hash
DEFAULT_SMTP_HOST = "localhost"
DEFAULT_SMTP_PORT = 25
DEFAULT_SMTP_TLS = False
DEFAULT_SMTP_SSL = False
def logo():
@ -71,11 +72,19 @@ def send_email(subject, recipient, text, html, attachements=None):
value, maintype=maintype, subtype=subtype, cid=cid
)
smtp = None
try:
with smtplib.SMTP(
if current_app.config["SMTP"].get("SSL", DEFAULT_SMTP_SSL):
smtp = smtplib.SMTP_SSL(
host=current_app.config["SMTP"].get("HOST", DEFAULT_SMTP_HOST),
port=current_app.config["SMTP"].get("PORT", DEFAULT_SMTP_PORT),
) as smtp:
)
else:
smtp = smtplib.SMTP(
host=current_app.config["SMTP"].get("HOST", DEFAULT_SMTP_HOST),
port=current_app.config["SMTP"].get("PORT", DEFAULT_SMTP_PORT),
)
if current_app.config["SMTP"].get("TLS", DEFAULT_SMTP_TLS):
smtp.starttls()
if current_app.config["SMTP"].get("LOGIN"):
@ -92,6 +101,10 @@ def send_email(subject, recipient, text, html, attachements=None):
current_app.logger.warning(f"Could not send email: {exc}")
return False
finally:
if smtp is not None:
smtp.close()
return True