forked from Github-Mirrors/canaille
Enable SSL SMTP
This commit is contained in:
parent
9d3cd71164
commit
4fa09b3a70
2 changed files with 26 additions and 12 deletions
|
@ -194,6 +194,7 @@ WEBSITE = "{{ user.labeledURI[0] }}"
|
||||||
# HOST = "localhost"
|
# HOST = "localhost"
|
||||||
# PORT = 25
|
# PORT = 25
|
||||||
# TLS = false
|
# TLS = false
|
||||||
|
# SSL = false
|
||||||
# LOGIN = ""
|
# LOGIN = ""
|
||||||
# PASSWORD = ""
|
# PASSWORD = ""
|
||||||
# FROM_ADDR = "admin@mydomain.tld"
|
# FROM_ADDR = "admin@mydomain.tld"
|
||||||
|
|
|
@ -17,6 +17,7 @@ from .apputils import profile_hash
|
||||||
DEFAULT_SMTP_HOST = "localhost"
|
DEFAULT_SMTP_HOST = "localhost"
|
||||||
DEFAULT_SMTP_PORT = 25
|
DEFAULT_SMTP_PORT = 25
|
||||||
DEFAULT_SMTP_TLS = False
|
DEFAULT_SMTP_TLS = False
|
||||||
|
DEFAULT_SMTP_SSL = False
|
||||||
|
|
||||||
|
|
||||||
def logo():
|
def logo():
|
||||||
|
@ -71,19 +72,27 @@ def send_email(subject, recipient, text, html, attachements=None):
|
||||||
value, maintype=maintype, subtype=subtype, cid=cid
|
value, maintype=maintype, subtype=subtype, cid=cid
|
||||||
)
|
)
|
||||||
|
|
||||||
|
smtp = None
|
||||||
try:
|
try:
|
||||||
with smtplib.SMTP(
|
if current_app.config["SMTP"].get("SSL", DEFAULT_SMTP_SSL):
|
||||||
host=current_app.config["SMTP"].get("HOST", DEFAULT_SMTP_HOST),
|
smtp = smtplib.SMTP_SSL(
|
||||||
port=current_app.config["SMTP"].get("PORT", DEFAULT_SMTP_PORT),
|
host=current_app.config["SMTP"].get("HOST", DEFAULT_SMTP_HOST),
|
||||||
) as smtp:
|
port=current_app.config["SMTP"].get("PORT", DEFAULT_SMTP_PORT),
|
||||||
if current_app.config["SMTP"].get("TLS", DEFAULT_SMTP_TLS):
|
)
|
||||||
smtp.starttls()
|
else:
|
||||||
if current_app.config["SMTP"].get("LOGIN"):
|
smtp = smtplib.SMTP(
|
||||||
smtp.login(
|
host=current_app.config["SMTP"].get("HOST", DEFAULT_SMTP_HOST),
|
||||||
user=current_app.config["SMTP"].get("LOGIN"),
|
port=current_app.config["SMTP"].get("PORT", DEFAULT_SMTP_PORT),
|
||||||
password=current_app.config["SMTP"].get("PASSWORD"),
|
)
|
||||||
)
|
|
||||||
smtp.send_message(msg)
|
if current_app.config["SMTP"].get("TLS", DEFAULT_SMTP_TLS):
|
||||||
|
smtp.starttls()
|
||||||
|
if current_app.config["SMTP"].get("LOGIN"):
|
||||||
|
smtp.login(
|
||||||
|
user=current_app.config["SMTP"].get("LOGIN"),
|
||||||
|
password=current_app.config["SMTP"].get("PASSWORD"),
|
||||||
|
)
|
||||||
|
smtp.send_message(msg)
|
||||||
|
|
||||||
except smtplib.SMTPRecipientsRefused:
|
except smtplib.SMTPRecipientsRefused:
|
||||||
pass
|
pass
|
||||||
|
@ -92,6 +101,10 @@ def send_email(subject, recipient, text, html, attachements=None):
|
||||||
current_app.logger.warning(f"Could not send email: {exc}")
|
current_app.logger.warning(f"Could not send email: {exc}")
|
||||||
return False
|
return False
|
||||||
|
|
||||||
|
finally:
|
||||||
|
if smtp is not None:
|
||||||
|
smtp.close()
|
||||||
|
|
||||||
return True
|
return True
|
||||||
|
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue