1"""Magic-link e-mail (SMTP). In dev or without SMTP the link is logged instead."""23from __future__ import annotations45from email.message import EmailMessage67import aiosmtplib89from app.core.config import Settings10from app.core.logging import get_logger1112log = get_logger("mail")131415async def send_magic_link(settings: Settings, to: str, link: str) -> bool:16 if not settings.smtp_enabled:17 log.info("magic_link_not_sent_no_smtp", to_domain=to.split("@")[-1])18 return False19 msg = EmailMessage()20 msg["From"] = settings.SMTP_FROM21 msg["To"] = to22 msg["Subject"] = "Connexion à UQO-Chat"23 msg.set_content(24 "Bonjour,\n\nVoici ton lien de connexion à UQO-Chat (valide 20 minutes) :\n\n"25 f"{link}\n\nSi tu n'as pas demandé ce lien, ignore ce courriel.\n\n— UQO-Chat, "26 "tuteur IA IMM1003 · IMM1033")27 await aiosmtplib.send(28 msg, hostname=settings.SMTP_HOST, port=settings.SMTP_PORT,29 username=settings.SMTP_USER or None,30 password=settings.SMTP_PASSWORD.get_secret_value() or None,31 start_tls=settings.SMTP_PORT == 587)32 return True33