FROM_ADDR configuration option is not mandatory anymore

This commit is contained in:
Éloi Rivard 2022-11-14 18:59:07 +01:00
parent 0ae12cedd6
commit 154ec9fcd2
9 changed files with 59 additions and 30 deletions

View file

@ -34,6 +34,7 @@ Changed
- Dynamically generate the server metadata. Users won't have to copy and
manually edit ``oauth-authorizationserver.json`` and
``openid-configuration.json``. :pr:`71`
- The `FROM_ADDR` configuration option is not mandatory anymore. :pr:`73`
[0.0.12] - 2022-10-24
=====================

View file

@ -60,6 +60,16 @@ def default_fields():
return read, write
def get_current_domain():
if current_app.config["SMTP"].get("FROM_ADDR"):
return current_app.config["SMTP"]["FROM_ADDR"].split("@")[-1]
elif current_app.config.get("SERVER_NAME"):
return current_app.config.get("SERVER_NAME")
return request.host
def logo():
logo_url = current_app.config.get("LOGO")
if not logo_url:
@ -83,12 +93,11 @@ def logo():
logo_filename = None
logo_raw = None
domain = current_app.config["SMTP"]["FROM_ADDR"].split("@")[-1]
logo_cid = make_msgid(domain=domain)
logo_cid = make_msgid(domain=get_current_domain())
return logo_cid, logo_filename, logo_raw
def send_email(subject, recipient, text, html, sender=None, attachements=None):
def send_email(subject, recipient, text, html, attachements=None):
msg = email.message.EmailMessage()
msg.set_content(text)
msg.add_alternative(html, subtype="html")
@ -96,15 +105,14 @@ def send_email(subject, recipient, text, html, sender=None, attachements=None):
msg["Subject"] = subject
msg["To"] = f"<{recipient}>"
if sender:
msg["From"] = sender
elif current_app.config.get("NAME", "Canaille"):
msg["From"] = '"{}" <{}>'.format(
current_app.config.get("NAME", "Canaille"),
current_app.config["SMTP"]["FROM_ADDR"],
)
else:
msg["From"] = current_app.config["SMTP"]["FROM_ADDR"]
name = current_app.config.get("NAME", "Canaille")
address = current_app.config["SMTP"].get("FROM_ADDR")
if not address:
domain = get_current_domain()
address = f"admin@{domain}"
msg["From"] = f'"{name}" <{address}>'
attachements = attachements or []
for cid, filename, value in attachements:

View file

@ -186,4 +186,4 @@ WEBSITE = "{{ user.labeledURI[0] }}"
# TLS = false
# LOGIN = ""
# PASSWORD = ""
FROM_ADDR = "admin@mydomain.tld"
# FROM_ADDR = "admin@mydomain.tld"

View file

@ -192,4 +192,4 @@ WEBSITE = "{{ user.labeledURI[0] }}"
# TLS = false
# LOGIN = ""
# PASSWORD = ""
FROM_ADDR = "admin@mydomain.tld"
# FROM_ADDR = "admin@mydomain.tld"

View file

@ -192,4 +192,4 @@ WEBSITE = "{{ user.labeledURI[0] }}"
# TLS = false
# LOGIN = ""
# PASSWORD = ""
FROM_ADDR = "admin@mydomain.tld"
# FROM_ADDR = "admin@mydomain.tld"

View file

@ -252,4 +252,5 @@ Without this section Canaille will still be usable, but all the features related
*Optional.*
:FROM_ADDR:
*Required.* The mail address to use as the sender for Canaille emails.
*Optional.* The mail address to use as the sender for Canaille emails.
Defaults to `admin@<HOSTNAME>` where `HOSTNAME` is the current hostname.

View file

@ -147,6 +147,7 @@ def configuration(slapd_server, smtpd, keypair_path):
conf = {
"SECRET_KEY": gen_salt(24),
"LOGGING": {},
"LOGO": "/static/img/canaille-head.png",
"LDAP": {
"ROOT_DN": slapd_server.suffix,
"URI": slapd_server.ldap_uri,

View file

@ -1,14 +0,0 @@
def test_send_test_email(testclient, logged_admin, smtpd):
assert len(smtpd.messages) == 0
res = testclient.get("/admin/mail")
res.form["mail"] = "test@test.com"
res = res.form.submit()
assert len(smtpd.messages) == 1
def test_mails(testclient, logged_admin):
for base in ["password-init", "reset", "admin/admin@admin.com/invitation"]:
testclient.get(f"/admin/mail/{base}.html")
testclient.get(f"/admin/mail/{base}.txt")

32
tests/test_mails.py Normal file
View file

@ -0,0 +1,32 @@
def test_send_test_email(testclient, logged_admin, smtpd):
assert len(smtpd.messages) == 0
res = testclient.get("/admin/mail")
res.form["mail"] = "test@test.com"
res = res.form.submit()
assert len(smtpd.messages) == 1
def test_mails(testclient, logged_admin):
for base in ["password-init", "reset", "admin/admin@admin.com/invitation"]:
testclient.get(f"/admin/mail/{base}.html")
testclient.get(f"/admin/mail/{base}.txt")
def test_custom_from_addr(testclient, user, smtpd):
testclient.app.config["NAME"] = "My Canaille"
res = testclient.get("/reset", status=200)
res.form["login"] = "user"
res = res.form.submit(status=200)
assert smtpd.messages[0]["X-MailFrom"] == "admin@mydomain.tld"
assert smtpd.messages[0]["From"] == '"My Canaille" <admin@mydomain.tld>'
def test_default_from_addr(testclient, user, smtpd):
del testclient.app.config["SMTP"]["FROM_ADDR"]
res = testclient.get("/reset", status=200)
res.form["login"] = "user"
res = res.form.submit(status=200)
assert smtpd.messages[0]["X-MailFrom"] == "admin@localhost"
assert smtpd.messages[0]["From"] == '"Canaille" <admin@localhost>'