diff --git a/canaille/account.py b/canaille/account.py index ac4b5361..b43d9eb5 100644 --- a/canaille/account.py +++ b/canaille/account.py @@ -1,6 +1,8 @@ +import base64 import email.message import hashlib import smtplib +import urllib.request from flask import Blueprint, request, flash, url_for, current_app from flask import render_template, redirect @@ -109,6 +111,16 @@ def forgotten(): uid=user.uid[0], hash=profile_hash(user.uid[0], user.userPassword[0]), )[1:] + logo = None + logo_extension = None + if current_app.config.get("LOGO"): + logo_extension = current_app.config["LOGO"].split(".")[-1] + try: + with urllib.request.urlopen(current_app.config.get("LOGO")) as f: + logo = base64.b64encode(f.read()).decode("utf-8") + except (urllib.error.HTTPError, urllib.error.URLError): + pass + subject = _("Password reset on {website_name}").format( website_name=current_app.config.get("NAME", reset_url) ) @@ -124,7 +136,8 @@ def forgotten(): site_name=current_app.config.get("NAME", reset_url), site_url=current_app.config.get("URL", base_url), reset_url=reset_url, - logo=current_app.config.get("LOGO"), + logo=logo, + logo_extension=logo_extension, ) msg = email.message.EmailMessage() diff --git a/canaille/admin/mail.py b/canaille/admin/mail.py index a70459d6..04fc873a 100644 --- a/canaille/admin/mail.py +++ b/canaille/admin/mail.py @@ -1,3 +1,5 @@ +import base64 +import urllib.request from flask import Blueprint, render_template, current_app, request, url_for from canaille.flaskutils import admin_needed from canaille.account import profile_hash @@ -16,12 +18,23 @@ def reset_html(user): hash=profile_hash(user.uid[0], user.userPassword[0]), )[1:] + logo = None + logo_extension = None + if current_app.config.get("LOGO"): + logo_extension = current_app.config["LOGO"].split(".")[-1] + try: + with urllib.request.urlopen(current_app.config.get("LOGO")) as f: + logo = base64.b64encode(f.read()).decode("utf-8") + except (urllib.error.HTTPError, urllib.error.URLError): + pass + return render_template( "mail/reset.html", site_name=current_app.config.get("NAME", reset_url), site_url=current_app.config.get("URL", base_url), reset_url=reset_url, - logo=current_app.config.get("LOGO"), + logo=logo, + logo_extension=logo_extension, ) diff --git a/canaille/templates/mail/reset.html b/canaille/templates/mail/reset.html index b9cd3bc4..0aaf3855 100644 --- a/canaille/templates/mail/reset.html +++ b/canaille/templates/mail/reset.html @@ -22,7 +22,7 @@ body {

{% if logo %} - {{ site_name }} + {{ site_name }} {% endif %}
{% trans %}Password reinitialisation{% endtrans %} diff --git a/tests/test_mail_admin.py b/tests/test_mail_admin.py index f0fbd68b..92229faf 100644 --- a/tests/test_mail_admin.py +++ b/tests/test_mail_admin.py @@ -1,5 +1,6 @@ def test_reset_html(testclient, logged_admin): testclient.get("/admin/mail/reset.html") + def test_reset_txt(testclient, logged_admin): testclient.get("/admin/mail/reset.txt")