import aiosmtplib from email.mime.multipart import MIMEMultipart from email.mime.text import MIMEText from app.config import get_settings async def send_email(to: str, subject: str, html_body: str, text_body: str | None = None): settings = get_settings() if not settings.SMTP_HOST: return # Email non configuré, on ignore silencieusement message = MIMEMultipart("alternative") message["Subject"] = subject message["From"] = settings.EMAIL_FROM message["To"] = to if text_body: message.attach(MIMEText(text_body, "plain")) message.attach(MIMEText(html_body, "html")) try: async with aiosmtplib.SMTP( hostname=settings.SMTP_HOST, port=settings.SMTP_PORT, start_tls=True, ) as smtp: await smtp.login(settings.SMTP_USER, settings.SMTP_PASSWORD) await smtp.send_message(message) except Exception: pass # Ne jamais laisser un échec d'email planter la requête async def send_booking_confirmed(to: str, booking_date: str, booking_time: str): settings = get_settings() html = f"""
Bonjour,
Votre rendez-vous chez {settings.BUSINESS_NAME} est confirmé.
Date : {booking_date}
Heure : {booking_time}
Nous vous contacterons pour finaliser le paiement. À très bientôt !
— L'équipe {settings.BUSINESS_NAME}
""" await send_email(to, f"Rendez-vous confirmé – {settings.BUSINESS_NAME}", html) async def send_booking_cancelled(to: str, booking_date: str, booking_time: str): settings = get_settings() html = f"""Bonjour,
Votre rendez-vous du {booking_date} à {booking_time} a été annulé.
Pour toute question, n'hésitez pas à nous contacter.
— L'équipe {settings.BUSINESS_NAME}
""" await send_email(to, f"Rendez-vous annulé – {settings.BUSINESS_NAME}", html) async def send_order_confirmed(to: str, order_id: str, total: float): settings = get_settings() html = f"""Bonjour,
Merci pour votre commande chez {settings.BUSINESS_NAME}.
Numéro de commande : {order_id}
Total : {total:.2f} €
Nous vous contacterons pour confirmer les modalités de livraison et de paiement.
— L'équipe {settings.BUSINESS_NAME}
""" await send_email(to, f"Commande confirmée – {settings.BUSINESS_NAME}", html)