Base64 logo in emails. Fixes #26

This commit is contained in:
Éloi Rivard 2020-10-29 12:59:18 +01:00
parent 552728a04e
commit dfa98cb656
4 changed files with 30 additions and 3 deletions

View file

@ -1,6 +1,8 @@
import base64
import email.message import email.message
import hashlib import hashlib
import smtplib import smtplib
import urllib.request
from flask import Blueprint, request, flash, url_for, current_app from flask import Blueprint, request, flash, url_for, current_app
from flask import render_template, redirect from flask import render_template, redirect
@ -109,6 +111,16 @@ def forgotten():
uid=user.uid[0], uid=user.uid[0],
hash=profile_hash(user.uid[0], user.userPassword[0]), hash=profile_hash(user.uid[0], user.userPassword[0]),
)[1:] )[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( subject = _("Password reset on {website_name}").format(
website_name=current_app.config.get("NAME", reset_url) 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_name=current_app.config.get("NAME", reset_url),
site_url=current_app.config.get("URL", base_url), site_url=current_app.config.get("URL", base_url),
reset_url=reset_url, reset_url=reset_url,
logo=current_app.config.get("LOGO"), logo=logo,
logo_extension=logo_extension,
) )
msg = email.message.EmailMessage() msg = email.message.EmailMessage()

View file

@ -1,3 +1,5 @@
import base64
import urllib.request
from flask import Blueprint, render_template, current_app, request, url_for from flask import Blueprint, render_template, current_app, request, url_for
from canaille.flaskutils import admin_needed from canaille.flaskutils import admin_needed
from canaille.account import profile_hash from canaille.account import profile_hash
@ -16,12 +18,23 @@ def reset_html(user):
hash=profile_hash(user.uid[0], user.userPassword[0]), hash=profile_hash(user.uid[0], user.userPassword[0]),
)[1:] )[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( return render_template(
"mail/reset.html", "mail/reset.html",
site_name=current_app.config.get("NAME", reset_url), site_name=current_app.config.get("NAME", reset_url),
site_url=current_app.config.get("URL", base_url), site_url=current_app.config.get("URL", base_url),
reset_url=reset_url, reset_url=reset_url,
logo=current_app.config.get("LOGO"), logo=logo,
logo_extension=logo_extension,
) )

View file

@ -22,7 +22,7 @@ body {
<div class="ui segment" style="line-height: 1.4285em; color: rgba(0,0,0,.87); font-family: 'Lato', sans-serif; font-weight: 400; box-sizing: inherit; position: relative; background: #fff; box-shadow: 0 1px 2px 0 rgba(34,36,38,.15); margin: 1rem 0; padding: 1em 1em; border-radius: .28571429rem; border: 1px solid rgba(34,36,38,.15); font-size: 1rem; margin-top: 0; margin-bottom: 0;"> <div class="ui segment" style="line-height: 1.4285em; color: rgba(0,0,0,.87); font-family: 'Lato', sans-serif; font-weight: 400; box-sizing: inherit; position: relative; background: #fff; box-shadow: 0 1px 2px 0 rgba(34,36,38,.15); margin: 1rem 0; padding: 1em 1em; border-radius: .28571429rem; border: 1px solid rgba(34,36,38,.15); font-size: 1rem; margin-top: 0; margin-bottom: 0;">
<h3 class="ui top attached header" style="box-sizing: inherit; font-family: Lato,'Helvetica Neue',Arial,Helvetica,sans-serif; font-weight: 700; line-height: 1.28571429em; text-transform: none; color: rgba(0,0,0,.87); font-size: 1.28571429rem; background: #fff; padding: .78571429rem 1rem; margin: 0 -1px 0 -1px; box-shadow: none; border: 1px solid #d4d4d5; border-radius: .28571429rem .28571429rem 0 0;"> <h3 class="ui top attached header" style="box-sizing: inherit; font-family: Lato,'Helvetica Neue',Arial,Helvetica,sans-serif; font-weight: 700; line-height: 1.28571429em; text-transform: none; color: rgba(0,0,0,.87); font-size: 1.28571429rem; background: #fff; padding: .78571429rem 1rem; margin: 0 -1px 0 -1px; box-shadow: none; border: 1px solid #d4d4d5; border-radius: .28571429rem .28571429rem 0 0;">
{% if logo %} {% if logo %}
<img class="ui image" src="{{ logo }}" alt="{{ site_name }}" style="font-family: Lato,'Helvetica Neue',Arial,Helvetica,sans-serif; font-weight: 700; line-height: 1.28571429em; text-transform: none; color: rgba(0,0,0,.87); font-size: 1.28571429rem; box-sizing: inherit; border-style: none; position: relative; max-width: 50px; max-height:50px; background-color: transparent; display: inline-block; margin-top: .14285714em; width: 2.5em; height: auto; vertical-align: middle;"> <img class="ui image" src="data:image/{{ logo_extension }};base64, {{ logo }}" alt="{{ site_name }}" style="font-family: Lato,'Helvetica Neue',Arial,Helvetica,sans-serif; font-weight: 700; line-height: 1.28571429em; text-transform: none; color: rgba(0,0,0,.87); font-size: 1.28571429rem; box-sizing: inherit; border-style: none; position: relative; max-width: 50px; max-height:50px; background-color: transparent; display: inline-block; margin-top: .14285714em; width: 2.5em; height: auto; vertical-align: middle;">
{% endif %} {% endif %}
<div class="content" style="font-family: Lato,'Helvetica Neue',Arial,Helvetica,sans-serif; font-weight: 700; line-height: 1.28571429em; text-transform: none; color: rgba(0,0,0,.87); font-size: 1.28571429rem; box-sizing: inherit; display: inline-block; padding-left: .75rem; vertical-align: middle;"> <div class="content" style="font-family: Lato,'Helvetica Neue',Arial,Helvetica,sans-serif; font-weight: 700; line-height: 1.28571429em; text-transform: none; color: rgba(0,0,0,.87); font-size: 1.28571429rem; box-sizing: inherit; display: inline-block; padding-left: .75rem; vertical-align: middle;">
{% trans %}Password reinitialisation{% endtrans %} {% trans %}Password reinitialisation{% endtrans %}

View file

@ -1,5 +1,6 @@
def test_reset_html(testclient, logged_admin): def test_reset_html(testclient, logged_admin):
testclient.get("/admin/mail/reset.html") testclient.get("/admin/mail/reset.html")
def test_reset_txt(testclient, logged_admin): def test_reset_txt(testclient, logged_admin):
testclient.get("/admin/mail/reset.txt") testclient.get("/admin/mail/reset.txt")