mirror of
http://88.130.71.182:3000/BlitTech/badoHair_be.git
synced 2026-06-12 23:23:22 +00:00
72 lines
2.6 KiB
Python
72 lines
2.6 KiB
Python
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"""
|
||
<h2>Votre rendez-vous est confirmé !</h2>
|
||
<p>Bonjour,</p>
|
||
<p>Votre rendez-vous chez <strong>{settings.BUSINESS_NAME}</strong> est confirmé.</p>
|
||
<p><strong>Date :</strong> {booking_date}<br>
|
||
<strong>Heure :</strong> {booking_time}</p>
|
||
<p>Nous vous contacterons pour finaliser le paiement. À très bientôt !</p>
|
||
<p>— L'équipe {settings.BUSINESS_NAME}</p>
|
||
"""
|
||
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"""
|
||
<h2>Annulation de votre rendez-vous</h2>
|
||
<p>Bonjour,</p>
|
||
<p>Votre rendez-vous du {booking_date} à {booking_time} a été annulé.</p>
|
||
<p>Pour toute question, n'hésitez pas à nous contacter.</p>
|
||
<p>— L'équipe {settings.BUSINESS_NAME}</p>
|
||
"""
|
||
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"""
|
||
<h2>Commande enregistrée !</h2>
|
||
<p>Bonjour,</p>
|
||
<p>Merci pour votre commande chez <strong>{settings.BUSINESS_NAME}</strong>.</p>
|
||
<p><strong>Numéro de commande :</strong> {order_id}<br>
|
||
<strong>Total :</strong> {total:.2f} €</p>
|
||
<p>Nous vous contacterons pour confirmer les modalités de livraison et de paiement.</p>
|
||
<p>— L'équipe {settings.BUSINESS_NAME}</p>
|
||
"""
|
||
await send_email(to, f"Commande confirmée – {settings.BUSINESS_NAME}", html)
|