"""Simple SMTP email service.""" import smtplib import logging from email.mime.text import MIMEText from email.mime.multipart import MIMEMultipart from app.config import settings logger = logging.getLogger(__name__) def send_email(to: str, subject: str, body: str, html: str | None = None) -> bool: """Send email via configured SMTP. Returns True on success.""" if not settings.SMTP_HOST: logger.warning(f"SMTP not configured — would send to {to}: {subject}") return False try: msg = MIMEMultipart("alternative") msg["Subject"] = subject msg["From"] = settings.SMTP_FROM msg["To"] = to msg.attach(MIMEText(body, "plain")) if html: msg.attach(MIMEText(html, "html")) with smtplib.SMTP(settings.SMTP_HOST, settings.SMTP_PORT) as smtp: smtp.starttls() if settings.SMTP_USER: smtp.login(settings.SMTP_USER, settings.SMTP_PASSWORD) smtp.sendmail(settings.SMTP_FROM, [to], msg.as_string()) logger.info(f"Email sent to {to}: {subject}") return True except Exception as e: logger.error(f"Email failed to {to}: {e}") return False