From: Michael Tremer Date: Thu, 26 Oct 2023 08:13:57 +0000 (+0000) Subject: messages: Automatically inline any CSS X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=20f9c733037bdfe1cb0affe932b0b610847251c6;p=ipfire.org.git messages: Automatically inline any CSS Signed-off-by: Michael Tremer --- diff --git a/configure.ac b/configure.ac index bbd7abdf..ae6a9af1 100644 --- a/configure.ac +++ b/configure.ac @@ -40,6 +40,7 @@ AX_PYTHON_MODULE([panoramisk], [fatal]) AX_PYTHON_MODULE([phonenumbers], [fatal]) AX_PYTHON_MODULE([psycopg], [fatal]) AX_PYTHON_MODULE([pycares], [fatal]) +AX_PYTHON_MODULE([pynliner], [fatal]) AX_PYTHON_MODULE([redis.asyncio], [fatal]) AX_PYTHON_MODULE([tornado], [fatal]) AX_PYTHON_MODULE([zxcvbn], [fatal]) diff --git a/src/backend/messages.py b/src/backend/messages.py index 0aea14e8..53f62baa 100644 --- a/src/backend/messages.py +++ b/src/backend/messages.py @@ -8,6 +8,7 @@ import email.utils import logging import mimetypes import os.path +import pynliner import random import smtplib import socket @@ -148,6 +149,10 @@ class Messages(misc.Object): except KeyError: message.add_header(header, value) + # Inline any CSS + if extension == "html": + message_part = self._inline_css(message_part) + # Create a MIMEText object out of it message_part = email.mime.text.MIMEText( message_part.get_payload(), mimetype) @@ -164,6 +169,24 @@ class Messages(misc.Object): if self.backend.debug: self.template_loader.reset() + def _inline_css(self, part): + """ + Inlines any CSS into style attributes + """ + # Fetch the payload + payload = part.get_payload() + + # Setup Pynliner + p = pynliner.Pynliner().from_string(payload) + + # Run the inlining + payload = p.run() + + # Set the payload again + part.set_payload(payload) + + return part + def embed_image(self, path): static_dir = self.backend.config.get("global", "static_dir") assert static_dir