- Formulaire email : overlay fullscreen au lieu d'inline (debordait de l'ecran) - Historique emails : stocke chaque adresse dans data/emails_history.json - Suggestions : affiche les emails deja saisis en pills cliquables, filtrees au fur et a mesure de la saisie - API GET /api/emails/historique + DELETE pour reset Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
97 lines
2.9 KiB
Python
97 lines
2.9 KiB
Python
import json
|
|
import logging
|
|
import smtplib
|
|
from email.mime.base import MIMEBase
|
|
from email.mime.multipart import MIMEMultipart
|
|
from email.mime.text import MIMEText
|
|
from email import encoders
|
|
from pathlib import Path
|
|
|
|
from backend.config import charger_config, RACINE
|
|
|
|
log = logging.getLogger("photobooth.mailer")
|
|
|
|
|
|
def envoyer_photo(destinataire: str, chemin_photo: Path) -> bool:
|
|
"""Envoie une photo par email."""
|
|
config = charger_config()
|
|
conf_email = config.get("email", {})
|
|
conf_event = config.get("evenement", {})
|
|
|
|
smtp_host = conf_email.get("smtp_host", "")
|
|
smtp_port = conf_email.get("smtp_port", 587)
|
|
smtp_user = conf_email.get("smtp_user", "")
|
|
smtp_password = conf_email.get("smtp_password", "")
|
|
expediteur = conf_email.get("expediteur", smtp_user)
|
|
|
|
if not smtp_host or not smtp_user:
|
|
log.error("Configuration email incomplete")
|
|
return False
|
|
|
|
sujet = conf_email.get("sujet", "Votre photo - {evenement}").format(
|
|
evenement=conf_event.get("nom", "Photobooth")
|
|
)
|
|
message = conf_email.get("message", "Voici votre photo !").format(
|
|
evenement=conf_event.get("nom", "Photobooth")
|
|
)
|
|
|
|
msg = MIMEMultipart()
|
|
msg["From"] = expediteur
|
|
msg["To"] = destinataire
|
|
msg["Subject"] = sujet
|
|
msg.attach(MIMEText(message, "plain", "utf-8"))
|
|
|
|
# Piece jointe
|
|
if chemin_photo.exists():
|
|
with open(chemin_photo, "rb") as f:
|
|
piece = MIMEBase("image", "jpeg")
|
|
piece.set_payload(f.read())
|
|
encoders.encode_base64(piece)
|
|
piece.add_header(
|
|
"Content-Disposition",
|
|
f"attachment; filename={chemin_photo.name}"
|
|
)
|
|
msg.attach(piece)
|
|
|
|
try:
|
|
with smtplib.SMTP(smtp_host, smtp_port, timeout=10) as serveur:
|
|
serveur.starttls()
|
|
serveur.login(smtp_user, smtp_password)
|
|
serveur.send_message(msg)
|
|
log.info(f"Email envoye a {destinataire}")
|
|
_sauvegarder_email_historique(destinataire)
|
|
return True
|
|
except (smtplib.SMTPException, OSError) as e:
|
|
log.error(f"Erreur envoi email : {e}")
|
|
return False
|
|
|
|
|
|
FICHIER_EMAILS = RACINE / "data" / "emails_history.json"
|
|
|
|
|
|
def _sauvegarder_email_historique(email: str):
|
|
historique = charger_emails_historique()
|
|
email_lower = email.lower().strip()
|
|
if email_lower not in historique:
|
|
historique.append(email_lower)
|
|
try:
|
|
with open(FICHIER_EMAILS, "w", encoding="utf-8") as f:
|
|
json.dump(historique, f, ensure_ascii=False)
|
|
except OSError:
|
|
pass
|
|
|
|
|
|
def charger_emails_historique() -> list:
|
|
if not FICHIER_EMAILS.exists():
|
|
return []
|
|
try:
|
|
with open(FICHIER_EMAILS, "r", encoding="utf-8") as f:
|
|
return json.load(f)
|
|
except (json.JSONDecodeError, OSError):
|
|
return []
|
|
|
|
|
|
def effacer_emails_historique():
|
|
if FICHIER_EMAILS.exists():
|
|
FICHIER_EMAILS.unlink()
|