SMTP SSL fixes

This commit is contained in:
Éloi Rivard 2023-02-28 10:07:08 +01:00
parent 7909837317
commit a57c86cc2c
6 changed files with 46 additions and 23 deletions

View file

@ -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
*****

View file

@ -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

View file

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

View file

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

View file

@ -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.*

View file

@ -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"]