forked from Github-Mirrors/canaille
SMTP SSL fixes
This commit is contained in:
parent
7909837317
commit
a57c86cc2c
6 changed files with 46 additions and 23 deletions
|
@ -13,6 +13,7 @@ Added
|
|||
and their consents can be revoked. :issue:`69` :pr:`103`
|
||||
- A ``populate`` command can be used to fill the database with
|
||||
random users generated with faker. :pr:`105`
|
||||
- SMTP SSL support. :pr:`108`
|
||||
|
||||
Fixed
|
||||
*****
|
||||
|
|
|
@ -74,25 +74,23 @@ def send_email(subject, recipient, text, html, attachements=None):
|
|||
|
||||
smtp = None
|
||||
try:
|
||||
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),
|
||||
)
|
||||
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"):
|
||||
smtp.login(
|
||||
user=current_app.config["SMTP"].get("LOGIN"),
|
||||
password=current_app.config["SMTP"].get("PASSWORD"),
|
||||
)
|
||||
smtp.send_message(msg)
|
||||
connection_func = (
|
||||
smtplib.SMTP_SSL
|
||||
if current_app.config["SMTP"].get("SSL", DEFAULT_SMTP_SSL)
|
||||
else smtplib.SMTP
|
||||
)
|
||||
with connection_func(
|
||||
host=current_app.config["SMTP"].get("HOST", DEFAULT_SMTP_HOST),
|
||||
port=current_app.config["SMTP"].get("PORT", DEFAULT_SMTP_PORT),
|
||||
) as smtp:
|
||||
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:
|
||||
pass
|
||||
|
@ -101,10 +99,6 @@ 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
|
||||
|
||||
|
||||
|
|
|
@ -200,6 +200,7 @@ WEBSITE = "{{ user.labeledURI[0] }}"
|
|||
# HOST = "localhost"
|
||||
# PORT = 25
|
||||
# TLS = false
|
||||
# SSL = false
|
||||
# LOGIN = ""
|
||||
# PASSWORD = ""
|
||||
# FROM_ADDR = "admin@mydomain.tld"
|
||||
|
|
|
@ -201,6 +201,7 @@ WEBSITE = "{{ user.labeledURI[0] }}"
|
|||
# HOST = "localhost"
|
||||
# PORT = 25
|
||||
# TLS = false
|
||||
# SSL = false
|
||||
# LOGIN = ""
|
||||
# PASSWORD = ""
|
||||
# FROM_ADDR = "admin@mydomain.tld"
|
||||
|
|
|
@ -244,6 +244,10 @@ Without this section Canaille will still be usable, but all the features related
|
|||
Whether the SMTP connection use TLS.
|
||||
Default to ``False``
|
||||
|
||||
:SSL:
|
||||
Whether the SMTP connection use SSL.
|
||||
Default to ``False``
|
||||
|
||||
:LOGIN:
|
||||
The SMTP server authentication login.
|
||||
*Optional.*
|
||||
|
|
|
@ -21,6 +21,28 @@ def test_send_test_email(testclient, logged_admin, smtpd):
|
|||
assert len(smtpd.messages) == 1
|
||||
|
||||
|
||||
def test_send_test_email_ssl(testclient, logged_admin, smtpd):
|
||||
smtpd.config.use_ssl = True
|
||||
smtpd.config.use_starttls = False
|
||||
|
||||
testclient.app.config["SMTP"]["SSL"] = True
|
||||
testclient.app.config["SMTP"]["TLS"] = False
|
||||
del testclient.app.config["SMTP"]["LOGIN"]
|
||||
del testclient.app.config["SMTP"]["PASSWORD"]
|
||||
|
||||
assert len(smtpd.messages) == 0
|
||||
|
||||
res = testclient.get("/admin/mail")
|
||||
res.form["mail"] = "test@test.com"
|
||||
res = res.form.submit()
|
||||
assert (
|
||||
"success",
|
||||
"The test invitation mail has been sent correctly",
|
||||
) in res.flashes
|
||||
|
||||
assert len(smtpd.messages) == 1
|
||||
|
||||
|
||||
def test_send_test_email_without_credentials(testclient, logged_admin, smtpd):
|
||||
del testclient.app.config["SMTP"]["LOGIN"]
|
||||
del testclient.app.config["SMTP"]["PASSWORD"]
|
||||
|
|
Loading…
Reference in a new issue