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