email_service.py 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639
  1. """Email service for sending authentication-related emails."""
  2. from __future__ import annotations
  3. import html
  4. import logging
  5. import re
  6. import secrets
  7. import smtplib
  8. import string
  9. from datetime import datetime, timezone
  10. from email.mime.multipart import MIMEMultipart
  11. from email.mime.text import MIMEText
  12. from typing import Any
  13. from sqlalchemy import select
  14. from sqlalchemy.ext.asyncio import AsyncSession
  15. from backend.app.models.notification_template import NotificationTemplate
  16. from backend.app.models.settings import Settings
  17. from backend.app.schemas.auth import SMTPSettings
  18. logger = logging.getLogger(__name__)
  19. def generate_secure_password(length: int = 16) -> str:
  20. """Generate a secure random password.
  21. Args:
  22. length: Length of the password (default: 16)
  23. Returns:
  24. A secure random password containing uppercase, lowercase, digits, and special characters
  25. """
  26. # Define character sets
  27. lowercase = string.ascii_lowercase
  28. uppercase = string.ascii_uppercase
  29. digits = string.digits
  30. special = "!@#$%^&*()_+-=[]{}|;:,.<>?"
  31. # Ensure at least one character from each set
  32. password_chars = [
  33. secrets.choice(lowercase),
  34. secrets.choice(uppercase),
  35. secrets.choice(digits),
  36. secrets.choice(special),
  37. ]
  38. # Fill the rest with random characters from all sets
  39. all_chars = lowercase + uppercase + digits + special
  40. password_chars.extend(secrets.choice(all_chars) for _ in range(length - 4))
  41. # Shuffle with CSPRNG — random.shuffle() is seeded from time and not cryptographically safe
  42. secrets.SystemRandom().shuffle(password_chars)
  43. return "".join(password_chars)
  44. async def get_notification_template(db: AsyncSession, event_type: str) -> NotificationTemplate | None:
  45. """Get a notification template by event type from database.
  46. Args:
  47. db: Database session
  48. event_type: Type of event (e.g., 'user_created', 'password_reset')
  49. Returns:
  50. NotificationTemplate object or None if not found
  51. """
  52. result = await db.execute(select(NotificationTemplate).where(NotificationTemplate.event_type == event_type))
  53. return result.scalar_one_or_none()
  54. def render_template(template_str: str, variables: dict[str, Any]) -> str:
  55. """Render a template string with variables.
  56. Args:
  57. template_str: Template string with {variable} placeholders
  58. variables: Dictionary of variables to substitute
  59. Returns:
  60. Rendered template string
  61. """
  62. result = template_str
  63. for key, value in variables.items():
  64. result = result.replace("{" + key + "}", str(value) if value is not None else "")
  65. # Remove any remaining unreplaced placeholders (case-insensitive, alphanumeric + underscore)
  66. result = re.sub(r"\{[a-zA-Z0-9_]+\}", "", result)
  67. return result
  68. async def get_smtp_settings(db: AsyncSession) -> SMTPSettings | None:
  69. """Get SMTP settings from database.
  70. Args:
  71. db: Database session
  72. Returns:
  73. SMTPSettings object or None if not configured
  74. """
  75. # Fetch all SMTP-related settings
  76. result = await db.execute(
  77. select(Settings).where(
  78. Settings.key.in_(
  79. [
  80. "smtp_host",
  81. "smtp_port",
  82. "smtp_username",
  83. "smtp_password",
  84. "smtp_use_tls",
  85. "smtp_security",
  86. "smtp_auth_enabled",
  87. "smtp_from_email",
  88. "smtp_from_name",
  89. ]
  90. )
  91. )
  92. )
  93. settings_dict = {s.key: s.value for s in result.scalars().all()}
  94. # Check if minimum required settings are present
  95. required_keys = ["smtp_host", "smtp_port", "smtp_from_email"]
  96. if not all(key in settings_dict for key in required_keys):
  97. return None
  98. # Handle migration: convert old smtp_use_tls to smtp_security if needed
  99. smtp_security = settings_dict.get("smtp_security")
  100. if not smtp_security:
  101. # Migrate from old smtp_use_tls format
  102. smtp_use_tls = settings_dict.get("smtp_use_tls", "true").lower() == "true"
  103. smtp_security = "starttls" if smtp_use_tls else "ssl"
  104. smtp_auth_enabled = settings_dict.get("smtp_auth_enabled", "true").lower() == "true"
  105. return SMTPSettings(
  106. smtp_host=settings_dict["smtp_host"],
  107. smtp_port=int(settings_dict["smtp_port"]),
  108. smtp_username=settings_dict.get("smtp_username"),
  109. smtp_password=settings_dict.get("smtp_password"),
  110. smtp_security=smtp_security,
  111. smtp_auth_enabled=smtp_auth_enabled,
  112. smtp_from_email=settings_dict["smtp_from_email"],
  113. smtp_from_name=settings_dict.get("smtp_from_name", "BamBuddy"),
  114. )
  115. async def save_smtp_settings(db: AsyncSession, smtp_settings: SMTPSettings) -> None:
  116. """Save SMTP settings to database.
  117. Args:
  118. db: Database session
  119. smtp_settings: SMTP settings to save
  120. """
  121. from backend.app.core.db_dialect import upsert_setting
  122. settings_data = {
  123. "smtp_host": smtp_settings.smtp_host,
  124. "smtp_port": str(smtp_settings.smtp_port),
  125. "smtp_security": smtp_settings.smtp_security,
  126. "smtp_auth_enabled": "true" if smtp_settings.smtp_auth_enabled else "false",
  127. "smtp_from_email": smtp_settings.smtp_from_email,
  128. "smtp_from_name": smtp_settings.smtp_from_name,
  129. }
  130. # Only save username if auth is enabled or if provided
  131. if smtp_settings.smtp_username:
  132. settings_data["smtp_username"] = smtp_settings.smtp_username
  133. # Only save password if provided
  134. if smtp_settings.smtp_password:
  135. settings_data["smtp_password"] = smtp_settings.smtp_password
  136. for key, value in settings_data.items():
  137. await upsert_setting(db, Settings, key, value)
  138. def send_email(
  139. smtp_settings: SMTPSettings,
  140. to_email: str,
  141. subject: str,
  142. body_text: str,
  143. body_html: str | None = None,
  144. ) -> None:
  145. """Send an email using SMTP.
  146. Args:
  147. smtp_settings: SMTP configuration
  148. to_email: Recipient email address
  149. subject: Email subject
  150. body_text: Plain text body
  151. body_html: Optional HTML body
  152. Raises:
  153. Exception: If email sending fails
  154. """
  155. msg = MIMEMultipart("alternative")
  156. msg["From"] = f"{smtp_settings.smtp_from_name} <{smtp_settings.smtp_from_email}>"
  157. msg["To"] = to_email
  158. msg["Subject"] = subject
  159. # Attach plain text part
  160. msg.attach(MIMEText(body_text, "plain"))
  161. # Attach HTML part if provided
  162. if body_html:
  163. msg.attach(MIMEText(body_html, "html"))
  164. # Send email
  165. try:
  166. security = smtp_settings.smtp_security
  167. auth_enabled = smtp_settings.smtp_auth_enabled
  168. # Validate username is provided when authentication is enabled
  169. if auth_enabled and smtp_settings.smtp_password:
  170. if not smtp_settings.smtp_username:
  171. raise ValueError("SMTP username is required when authentication is enabled")
  172. if security == "ssl":
  173. # Direct SSL connection (typically port 465)
  174. with smtplib.SMTP_SSL(smtp_settings.smtp_host, smtp_settings.smtp_port, timeout=10) as server:
  175. if auth_enabled and smtp_settings.smtp_password and smtp_settings.smtp_username:
  176. server.login(smtp_settings.smtp_username, smtp_settings.smtp_password)
  177. server.send_message(msg)
  178. elif security == "starttls":
  179. # STARTTLS upgrade (typically port 587)
  180. with smtplib.SMTP(smtp_settings.smtp_host, smtp_settings.smtp_port, timeout=10) as server:
  181. server.starttls()
  182. if auth_enabled and smtp_settings.smtp_password and smtp_settings.smtp_username:
  183. server.login(smtp_settings.smtp_username, smtp_settings.smtp_password)
  184. server.send_message(msg)
  185. else:
  186. # No encryption (typically port 25) - use with caution
  187. with smtplib.SMTP(smtp_settings.smtp_host, smtp_settings.smtp_port, timeout=10) as server:
  188. if auth_enabled and smtp_settings.smtp_password and smtp_settings.smtp_username:
  189. server.login(smtp_settings.smtp_username, smtp_settings.smtp_password)
  190. server.send_message(msg)
  191. logger.info(f"Email sent successfully to {to_email}")
  192. except Exception as e:
  193. logger.error(f"Failed to send email to {to_email}: {e}")
  194. raise
  195. def create_welcome_email(username: str, password: str, login_url: str) -> tuple[str, str, str]:
  196. """Create welcome email content for new user.
  197. Args:
  198. username: Username of the new user
  199. password: Auto-generated password
  200. login_url: URL to login page
  201. Returns:
  202. Tuple of (subject, text_body, html_body)
  203. """
  204. subject = "Welcome to BamBuddy - Your Account Details"
  205. text_body = f"""Welcome to BamBuddy!
  206. Your account has been created. Here are your login details:
  207. Username: {username}
  208. Password: {password}
  209. You can login at: {login_url}
  210. For security reasons, please change your password after your first login.
  211. Best regards,
  212. BamBuddy Team
  213. """
  214. html_body = f"""<!DOCTYPE html>
  215. <html>
  216. <head>
  217. <meta charset="utf-8">
  218. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  219. </head>
  220. <body style="font-family: Arial, sans-serif; line-height: 1.6; color: #333; max-width: 600px; margin: 0 auto; padding: 20px;">
  221. <div style="background: linear-gradient(135deg, #667eea 0%, #764ba2 100%); background-color: #667eea; padding: 20px; border-radius: 8px 8px 0 0;">
  222. <h1 style="color: #ffffff; margin: 0; font-size: 24px; text-shadow: 0 1px 2px rgba(0,0,0,0.3);">Welcome to BamBuddy!</h1>
  223. </div>
  224. <div style="background: #f9f9f9; padding: 30px; border-radius: 0 0 8px 8px; border: 1px solid #ddd; border-top: none;">
  225. <p style="font-size: 16px;">Your account has been created. Here are your login details:</p>
  226. <div style="background: white; padding: 20px; border-radius: 4px; margin: 20px 0; border-left: 4px solid #667eea;">
  227. <p style="margin: 0 0 10px 0;"><strong>Username:</strong> <code style="background: #f0f0f0; padding: 2px 6px; border-radius: 3px;">{username}</code></p>
  228. <p style="margin: 0;"><strong>Password:</strong> <code style="background: #f0f0f0; padding: 2px 6px; border-radius: 3px;">{password}</code></p>
  229. </div>
  230. <div style="text-align: center; margin: 30px 0;">
  231. <a href="{login_url}" style="display: inline-block; background-color: #667eea; color: #ffffff; padding: 12px 30px; text-decoration: none; border-radius: 4px; font-weight: bold;">Login Now</a>
  232. </div>
  233. <p style="font-size: 14px; color: #666; border-top: 1px solid #ddd; padding-top: 20px; margin-top: 20px;">
  234. <strong>Security Note:</strong> For security reasons, please change your password after your first login.
  235. </p>
  236. <p style="font-size: 14px; color: #999; margin-top: 30px;">
  237. Best regards,<br>
  238. BamBuddy Team
  239. </p>
  240. </div>
  241. </body>
  242. </html>
  243. """
  244. return subject, text_body, html_body
  245. def create_password_reset_email(username: str, password: str, login_url: str) -> tuple[str, str, str]:
  246. """Create password reset email content.
  247. Args:
  248. username: Username of the user
  249. password: New auto-generated password
  250. login_url: URL to login page
  251. Returns:
  252. Tuple of (subject, text_body, html_body)
  253. """
  254. subject = "BamBuddy - Your Password Has Been Reset"
  255. text_body = f"""Your BamBuddy password has been reset.
  256. Your login details:
  257. Username: {username}
  258. New Password: {password}
  259. You can login at: {login_url}
  260. For security reasons, please change your password after logging in.
  261. If you did not request this password reset, please contact your administrator immediately.
  262. Best regards,
  263. BamBuddy Team
  264. """
  265. html_body = f"""<!DOCTYPE html>
  266. <html>
  267. <head>
  268. <meta charset="utf-8">
  269. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  270. </head>
  271. <body style="font-family: Arial, sans-serif; line-height: 1.6; color: #333; max-width: 600px; margin: 0 auto; padding: 20px;">
  272. <div style="background: linear-gradient(135deg, #667eea 0%, #764ba2 100%); background-color: #667eea; padding: 20px; border-radius: 8px 8px 0 0;">
  273. <h1 style="color: #ffffff; margin: 0; font-size: 24px; text-shadow: 0 1px 2px rgba(0,0,0,0.3);">Password Reset</h1>
  274. </div>
  275. <div style="background: #f9f9f9; padding: 30px; border-radius: 0 0 8px 8px; border: 1px solid #ddd; border-top: none;">
  276. <p style="font-size: 16px;">Your BamBuddy password has been reset.</p>
  277. <div style="background: white; padding: 20px; border-radius: 4px; margin: 20px 0; border-left: 4px solid #667eea;">
  278. <p style="margin: 0 0 10px 0;"><strong>Username:</strong> <code style="background: #f0f0f0; padding: 2px 6px; border-radius: 3px;">{username}</code></p>
  279. <p style="margin: 0;"><strong>New Password:</strong> <code style="background: #f0f0f0; padding: 2px 6px; border-radius: 3px;">{password}</code></p>
  280. </div>
  281. <div style="text-align: center; margin: 30px 0;">
  282. <a href="{login_url}" style="display: inline-block; background-color: #667eea; color: #ffffff; padding: 12px 30px; text-decoration: none; border-radius: 4px; font-weight: bold;">Login Now</a>
  283. </div>
  284. <div style="background-color: #fff3cd; border: 1px solid #ffc107; border-radius: 4px; padding: 15px; margin: 20px 0;">
  285. <p style="margin: 0; font-size: 14px; color: #856404;">
  286. <strong>⚠️ Security Alert:</strong> If you did not request this password reset, please contact your administrator immediately.
  287. </p>
  288. </div>
  289. <p style="font-size: 14px; color: #666; border-top: 1px solid #ddd; padding-top: 20px; margin-top: 20px;">
  290. <strong>Security Note:</strong> For security reasons, please change your password after logging in.
  291. </p>
  292. <p style="font-size: 14px; color: #999; margin-top: 30px;">
  293. Best regards,<br>
  294. BamBuddy Team
  295. </p>
  296. </div>
  297. </body>
  298. </html>
  299. """
  300. return subject, text_body, html_body
  301. def create_password_reset_link_email(username: str, reset_url: str) -> tuple[str, str, str]:
  302. """Create a password-reset email that contains a secure link (not a plaintext password)."""
  303. subject = "BamBuddy - Password Reset Request"
  304. text_body = f"""A password reset was requested for your BamBuddy account.
  305. Username: {username}
  306. Click the link below to set a new password (valid for 1 hour):
  307. {reset_url}
  308. If you did not request this reset, you can safely ignore this email.
  309. Best regards,
  310. BamBuddy Team
  311. """
  312. html_body = f"""<!DOCTYPE html>
  313. <html>
  314. <head>
  315. <meta charset="utf-8">
  316. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  317. </head>
  318. <body style="font-family: Arial, sans-serif; line-height: 1.6; color: #333; max-width: 600px; margin: 0 auto; padding: 20px;">
  319. <div style="background: linear-gradient(135deg, #667eea 0%, #764ba2 100%); background-color: #667eea; padding: 20px; border-radius: 8px 8px 0 0;">
  320. <h1 style="color: #ffffff; margin: 0; font-size: 24px;">Password Reset Request</h1>
  321. </div>
  322. <div style="background: #f9f9f9; padding: 30px; border-radius: 0 0 8px 8px; border: 1px solid #ddd; border-top: none;">
  323. <p style="font-size: 16px;">A password reset was requested for your BamBuddy account (<strong>{username}</strong>).</p>
  324. <p>Click the button below to set a new password. This link is valid for <strong>1 hour</strong>.</p>
  325. <div style="text-align: center; margin: 30px 0;">
  326. <a href="{reset_url}" style="display: inline-block; background-color: #667eea; color: #ffffff; padding: 12px 30px; text-decoration: none; border-radius: 4px; font-weight: bold;">Reset Password</a>
  327. </div>
  328. <div style="background-color: #fff3cd; border: 1px solid #ffc107; border-radius: 4px; padding: 15px; margin: 20px 0;">
  329. <p style="margin: 0; font-size: 14px; color: #856404;">
  330. <strong>Did not request this?</strong> You can safely ignore this email. Your password has not been changed.
  331. </p>
  332. </div>
  333. <p style="font-size: 14px; color: #999; margin-top: 30px;">
  334. Best regards,<br>BamBuddy Team
  335. </p>
  336. </div>
  337. </body>
  338. </html>
  339. """
  340. return subject, text_body, html_body
  341. async def create_password_reset_link_email_from_template(
  342. db: AsyncSession, username: str, reset_url: str
  343. ) -> tuple[str, str, str]:
  344. """Create password-reset link email, using DB template if configured."""
  345. template = await get_notification_template(db, "password_reset_link")
  346. if template:
  347. variables = {"username": username, "reset_url": reset_url}
  348. subject = render_template(template.subject or "BamBuddy - Password Reset Request", variables)
  349. text_body = render_template(template.body or "", variables)
  350. html_body = render_template(template.html_body or "", variables) if template.html_body else None
  351. if not html_body:
  352. _, text_body, html_body = create_password_reset_link_email(username, reset_url)
  353. return subject, text_body, html_body
  354. return subject, text_body, html_body
  355. return create_password_reset_link_email(username, reset_url)
  356. async def create_welcome_email_from_template(
  357. db: AsyncSession, username: str, password: str, login_url: str, app_name: str = "BamBuddy"
  358. ) -> tuple[str, str, str]:
  359. """Create welcome email content using notification template from database.
  360. Args:
  361. db: Database session
  362. username: Username of the new user
  363. password: Auto-generated password
  364. login_url: URL to login page
  365. app_name: Application name (default: BamBuddy)
  366. Returns:
  367. Tuple of (subject, text_body, html_body)
  368. """
  369. # Try to get template from database
  370. template = await get_notification_template(db, "user_created")
  371. if template:
  372. # Render template with variables
  373. variables = {
  374. "app_name": app_name,
  375. "username": username,
  376. "password": password,
  377. "login_url": login_url,
  378. }
  379. subject = render_template(template.title_template, variables)
  380. text_body = render_template(template.body_template, variables)
  381. # Create HTML version with embedded login button
  382. # Escape text_body to prevent XSS vulnerabilities and convert newlines to <br> tags
  383. escaped_text_body = html.escape(text_body).replace("\n", "<br>\n")
  384. html_body = f"""<!DOCTYPE html>
  385. <html>
  386. <head>
  387. <meta charset="utf-8">
  388. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  389. </head>
  390. <body style="font-family: Arial, sans-serif; line-height: 1.6; color: #333; max-width: 600px; margin: 0 auto; padding: 20px;">
  391. <div style="background: linear-gradient(135deg, #667eea 0%, #764ba2 100%); background-color: #667eea; padding: 30px; border-radius: 8px 8px 0 0;">
  392. <h1 style="color: #ffffff; margin: 0; font-size: 24px; text-shadow: 0 1px 2px rgba(0,0,0,0.3);">{html.escape(subject)}</h1>
  393. </div>
  394. <div style="background: #f9f9f9; padding: 30px; border-radius: 0 0 8px 8px; border: 1px solid #ddd; border-top: none;">
  395. <div style="font-size: 16px;">{escaped_text_body}</div>
  396. <div style="text-align: center; margin: 30px 0;">
  397. <a href="{login_url}" style="display: inline-block; background-color: #667eea; color: #ffffff; padding: 12px 30px; text-decoration: none; border-radius: 4px; font-weight: bold;">Login Now</a>
  398. </div>
  399. </div>
  400. </body>
  401. </html>
  402. """
  403. logger.info("Using custom welcome email template from database")
  404. return subject, text_body, html_body
  405. else:
  406. # Fallback to hardcoded template
  407. logger.warning("No welcome email template found in database, using default")
  408. return create_welcome_email(username, password, login_url)
  409. async def create_password_reset_email_from_template(
  410. db: AsyncSession, username: str, password: str, login_url: str, app_name: str = "BamBuddy"
  411. ) -> tuple[str, str, str]:
  412. """Create password reset email content using notification template from database.
  413. Args:
  414. db: Database session
  415. username: Username of the user
  416. password: New auto-generated password
  417. login_url: URL to login page
  418. app_name: Application name (default: BamBuddy)
  419. Returns:
  420. Tuple of (subject, text_body, html_body)
  421. """
  422. # Try to get template from database
  423. template = await get_notification_template(db, "password_reset")
  424. if template:
  425. # Render template with variables
  426. variables = {
  427. "app_name": app_name,
  428. "username": username,
  429. "password": password,
  430. "login_url": login_url,
  431. }
  432. subject = render_template(template.title_template, variables)
  433. text_body = render_template(template.body_template, variables)
  434. # Create HTML version with embedded login button
  435. # Escape text_body to prevent XSS vulnerabilities and convert newlines to <br> tags
  436. escaped_text_body = html.escape(text_body).replace("\n", "<br>\n")
  437. html_body = f"""<!DOCTYPE html>
  438. <html>
  439. <head>
  440. <meta charset="utf-8">
  441. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  442. </head>
  443. <body style="font-family: Arial, sans-serif; line-height: 1.6; color: #333; max-width: 600px; margin: 0 auto; padding: 20px;">
  444. <div style="background: linear-gradient(135deg, #667eea 0%, #764ba2 100%); background-color: #667eea; padding: 30px; border-radius: 8px 8px 0 0;">
  445. <h1 style="color: #ffffff; margin: 0; font-size: 24px; text-shadow: 0 1px 2px rgba(0,0,0,0.3);">{html.escape(subject)}</h1>
  446. </div>
  447. <div style="background: #f9f9f9; padding: 30px; border-radius: 0 0 8px 8px; border: 1px solid #ddd; border-top: none;">
  448. <div style="font-size: 16px;">{escaped_text_body}</div>
  449. <div style="text-align: center; margin: 30px 0;">
  450. <a href="{login_url}" style="display: inline-block; background-color: #667eea; color: #ffffff; padding: 12px 30px; text-decoration: none; border-radius: 4px; font-weight: bold;">Login Now</a>
  451. </div>
  452. <div style="background-color: #fff3cd; border: 1px solid #ffc107; border-radius: 4px; padding: 15px; margin: 20px 0;">
  453. <p style="margin: 0; font-size: 14px; color: #856404;">
  454. <strong>⚠️ Security Alert:</strong> If you did not request this password reset, please contact your administrator immediately.
  455. </p>
  456. </div>
  457. </div>
  458. </body>
  459. </html>
  460. """
  461. logger.info("Using custom password reset email template from database")
  462. return subject, text_body, html_body
  463. else:
  464. # Fallback to hardcoded template
  465. logger.warning("No password reset email template found in database, using default")
  466. return create_password_reset_email(username, password, login_url)
  467. async def send_user_print_notification(
  468. db: AsyncSession,
  469. event_type: str,
  470. user_email: str,
  471. username: str,
  472. variables: dict,
  473. ) -> None:
  474. """Send a print notification email to a user using Advanced Auth SMTP settings.
  475. Args:
  476. db: Database session
  477. event_type: One of 'user_print_start', 'user_print_complete', 'user_print_failed', 'user_print_stopped'
  478. user_email: Recipient email address
  479. username: Username of the recipient
  480. variables: Template variables (printer, filename, etc.)
  481. """
  482. # Check that advanced auth is enabled (SMTP settings must be configured)
  483. smtp_settings = await get_smtp_settings(db)
  484. if not smtp_settings:
  485. logger.warning("Cannot send user print notification: SMTP settings not configured")
  486. return
  487. # Get the template
  488. template = await get_notification_template(db, event_type)
  489. if template is None:
  490. logger.warning("No template found for event type: %s", event_type)
  491. return
  492. # Add common variables (username, timestamp, app_name) merged with caller-supplied variables
  493. all_variables = {
  494. "username": username,
  495. "timestamp": datetime.now(timezone.utc).strftime("%Y-%m-%d %H:%M UTC"),
  496. "app_name": "Bambuddy",
  497. **variables,
  498. }
  499. subject = render_template(template.title_template, all_variables)
  500. text_body = render_template(template.body_template, all_variables)
  501. # Build HTML body — content comes entirely from the database template
  502. escaped_text_body = html.escape(text_body).replace("\n", "<br>\n")
  503. html_body = f"""<!DOCTYPE html>
  504. <html>
  505. <head>
  506. <meta charset="utf-8">
  507. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  508. </head>
  509. <body style="font-family: Arial, sans-serif; line-height: 1.6; color: #333; max-width: 600px; margin: 0 auto; padding: 20px;">
  510. <div style="background: linear-gradient(135deg, #1db954 0%, #158a3e 100%); background-color: #1db954; padding: 20px; border-radius: 8px 8px 0 0;">
  511. <h1 style="color: #ffffff; margin: 0; font-size: 24px; text-shadow: 0 1px 2px rgba(0,0,0,0.3);">{html.escape(subject)}</h1>
  512. </div>
  513. <div style="background: #f9f9f9; padding: 30px; border-radius: 0 0 8px 8px; border: 1px solid #ddd; border-top: none;">
  514. <div style="font-size: 16px;">{escaped_text_body}</div>
  515. </div>
  516. </body>
  517. </html>
  518. """
  519. try:
  520. send_email(smtp_settings, user_email, subject, text_body, html_body)
  521. logger.info("Sent %s notification email to %s", event_type, user_email)
  522. except Exception as e:
  523. logger.error("Failed to send %s notification to %s: %s", event_type, user_email, e)