From 4e1c61040570ea9d15b6e814aa86e58df7becc83 Mon Sep 17 00:00:00 2001 From: Colin Maudry Date: Wed, 24 Jun 2026 03:43:55 +0200 Subject: [PATCH] =?UTF-8?q?feat(auth):=20d=C3=A9coupler=20MAIL=5FSUPPRESS?= =?UTF-8?q?=5FSEND=20de=20DEVELOPMENT=20et=20logger=20les=20emails=20suppr?= =?UTF-8?q?im=C3=A9s?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Ajoute la variable MAIL_SUPPRESS_SEND pour pouvoir tester l'envoi d'emails en mode développement sans toucher au flag DEVELOPMENT. Un warning est loggé à chaque tentative d'envoi supprimée avec la cause et les détails du message. Co-Authored-By: Claude Sonnet 4.6 --- src/auth/mailer.py | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/src/auth/mailer.py b/src/auth/mailer.py index f1e981a..c5fc0e0 100644 --- a/src/auth/mailer.py +++ b/src/auth/mailer.py @@ -4,6 +4,8 @@ from pathlib import Path from flask import Flask, render_template from flask_mail import Mail, Message +from src.utils import logger + TEMPLATES_DIR = Path(__file__).parent / "templates" _mail: Mail | None = None @@ -18,7 +20,8 @@ def init_mailer(app: Flask) -> None: app.config["MAIL_USE_TLS"] = os.getenv("SMTP_USE_TLS", "True").lower() == "true" app.config["MAIL_DEFAULT_SENDER"] = os.getenv("MAIL_FROM", "noreply@decp.info") app.config["MAIL_SUPPRESS_SEND"] = ( - os.getenv("DEVELOPMENT", "False").lower() == "true" + os.getenv("MAIL_SUPPRESS_SEND", os.getenv("DEVELOPMENT", "False")).lower() + == "true" ) # Inclure les templates du package auth dans la recherche Jinja if hasattr(app.jinja_loader, "searchpath"): @@ -44,6 +47,18 @@ def _send( **ctx, ) -> None: assert _mail is not None, "Mailer non initialisé (init_mailer(app) non appelé)" + if _mail.suppress: + suppress_cause = ( + "MAIL_SUPPRESS_SEND=true" + if os.getenv("MAIL_SUPPRESS_SEND") is not None + else "DEVELOPMENT=true" + ) + logger.warning( + "Email supprimé (%s) — non envoyé à %s : %s", + suppress_cause, + recipient, + subject, + ) msg = Message(subject=subject, recipients=[recipient]) msg.body = render_template(txt_template, **ctx) msg.html = render_template(html_template, **ctx)