import logging import smtplib from email.mime.multipart import MIMEMultipart from email.mime.text import MIMEText from app.config import settings logger = logging.getLogger(__name__) def _send_smtp(to_email: str, subject: str, body: str) -> None: if not settings.smtp_host: logger.warning( "SMTP not configured. Email to %s — subject: %s\n%s", to_email, subject, body, ) return msg = MIMEMultipart("alternative") msg["Subject"] = subject msg["From"] = settings.smtp_from or settings.smtp_user msg["To"] = to_email msg.attach(MIMEText(body, "plain", "utf-8")) with smtplib.SMTP(settings.smtp_host, settings.smtp_port) as server: if settings.smtp_use_tls: server.starttls() if settings.smtp_user and settings.smtp_password: server.login(settings.smtp_user, settings.smtp_password) server.sendmail(msg["From"], [to_email], msg.as_string()) def send_activation_email(to_email: str, username: str, code: str) -> None: activate_url = f"{settings.frontend_url.rstrip('/')}/activate" body = ( f"Hello {username},\n\n" f"Thank you for registering at Document Template Editor.\n\n" f"Your activation code: {code}\n\n" f"Enter this code at: {activate_url}\n\n" f"The code expires in {settings.activation_code_expire_hours} hours.\n" ) _send_smtp(to_email, "Activate your account", body) def send_password_reset_email(to_email: str, username: str, code: str) -> None: reset_url = f"{settings.frontend_url.rstrip('/')}/reset-password" body = ( f"Hello {username},\n\n" f"You requested a password reset.\n\n" f"Your reset code: {code}\n\n" f"Enter this code at: {reset_url}\n\n" f"The code expires in {settings.password_reset_code_expire_hours} hours.\n\n" f"If you did not request this, ignore this email.\n" ) _send_smtp(to_email, "Password reset code", body)