feat(auth): découpler MAIL_SUPPRESS_SEND de DEVELOPMENT et logger les emails supprimés
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 <noreply@anthropic.com>
This commit is contained in:
+16
-1
@@ -4,6 +4,8 @@ from pathlib import Path
|
|||||||
from flask import Flask, render_template
|
from flask import Flask, render_template
|
||||||
from flask_mail import Mail, Message
|
from flask_mail import Mail, Message
|
||||||
|
|
||||||
|
from src.utils import logger
|
||||||
|
|
||||||
TEMPLATES_DIR = Path(__file__).parent / "templates"
|
TEMPLATES_DIR = Path(__file__).parent / "templates"
|
||||||
|
|
||||||
_mail: Mail | None = None
|
_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_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_DEFAULT_SENDER"] = os.getenv("MAIL_FROM", "noreply@decp.info")
|
||||||
app.config["MAIL_SUPPRESS_SEND"] = (
|
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
|
# Inclure les templates du package auth dans la recherche Jinja
|
||||||
if hasattr(app.jinja_loader, "searchpath"):
|
if hasattr(app.jinja_loader, "searchpath"):
|
||||||
@@ -44,6 +47,18 @@ def _send(
|
|||||||
**ctx,
|
**ctx,
|
||||||
) -> None:
|
) -> None:
|
||||||
assert _mail is not None, "Mailer non initialisé (init_mailer(app) non appelé)"
|
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 = Message(subject=subject, recipients=[recipient])
|
||||||
msg.body = render_template(txt_template, **ctx)
|
msg.body = render_template(txt_template, **ctx)
|
||||||
msg.html = render_template(html_template, **ctx)
|
msg.html = render_template(html_template, **ctx)
|
||||||
|
|||||||
Reference in New Issue
Block a user