import html from dataclasses import dataclass from urllib.parse import urlparse WELCOME_SUBJECT = "Welcome to MathPulse AI - Your Account Details" ACCENT_COLOR = "#9956DE" @dataclass class WelcomeCredentialsEmailContext: recipient_name: str login_email: str temporary_password: str role: str login_url: str brand_avatar_url: str = "" recipient_avatar_url: str = "" def _normalize_display_name(name: str) -> str: cleaned = (name or "").strip() return cleaned or "Learner" def _normalize_http_url(url: str) -> str: candidate = (url or "").strip() if not candidate: return "" parsed = urlparse(candidate) if parsed.scheme.lower() not in {"http", "https"}: return "" if not parsed.netloc: return "" return candidate def build_welcome_credentials_email(context: WelcomeCredentialsEmailContext) -> dict: recipient_name = _normalize_display_name(context.recipient_name) login_email = (context.login_email or "").strip() temporary_password = (context.temporary_password or "").strip() role = (context.role or "").strip() or "User" login_url = _normalize_http_url(context.login_url) or "https://mathpulse.ai" brand_avatar_url = _normalize_http_url(context.brand_avatar_url) recipient_avatar_url = _normalize_http_url(context.recipient_avatar_url) escaped_name = html.escape(recipient_name) escaped_email = html.escape(login_email) escaped_password = html.escape(temporary_password) escaped_role = html.escape(role) escaped_url = html.escape(login_url, quote=True) escaped_brand_avatar_url = html.escape(brand_avatar_url, quote=True) escaped_recipient_avatar_url = html.escape(recipient_avatar_url, quote=True) recipient_initial = html.escape((recipient_name[:1] or "U").upper()) if escaped_brand_avatar_url: brand_avatar_markup = ( f'MathPulse avatar' ) else: brand_avatar_markup = ( '
MP
' ) if escaped_recipient_avatar_url: recipient_avatar_markup = ( f'Learner avatar' ) else: recipient_avatar_markup = ( '
{recipient_initial}
' ) html_content = f""" {WELCOME_SUBJECT}
{brand_avatar_markup}

MathPulse AI

Learning Platform Account Access

{recipient_avatar_markup}

Hello {escaped_name},

Welcome to MathPulse AI. Your account has been created by your administrator. Use the credentials below to sign in and begin your learning journey.

Email: {escaped_email}
Temporary Password: {escaped_password}
Role: {escaped_role}
Log in to MathPulse

Security note: Please change your password after your first login.

If you did not expect this email, please contact your administrator.

""".strip() text_content = ( "MathPulse AI\n\n" f"Hello {recipient_name},\n\n" "Welcome to MathPulse AI. Your account has been created by your administrator.\n\n" "Account details:\n" f"- Email: {login_email}\n" f"- Temporary Password: {temporary_password}\n" f"- Role: {role}\n\n" f"Log in here: {login_url}\n\n" "Security note: Please change your password after your first login.\n\n" "If you did not expect this email, please contact your administrator.\n" ) return { "subject": WELCOME_SUBJECT, "html": html_content, "text": text_content, }