feat: wire SMTP email service, production CORS domains, Razorpay ENV, MinIO env-vars, and DPS seed from admin_uploads

This commit is contained in:
2026-09-09 09:48:00 +05:30
parent 5951ab5b3f
commit 63c8067216
3 changed files with 227 additions and 11 deletions

138
backend/email_service.py Normal file
View File

@@ -0,0 +1,138 @@
"""
email_service.py — PragatiVeer Transactional Email Service
Uses SMTP credentials from environment variables.
Falls back to console-log mode if SMTP is not configured.
"""
import os
import smtplib
import logging
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from typing import Optional
logger = logging.getLogger("email_service")
SMTP_HOST = os.getenv("SMTP_HOST", "")
SMTP_PORT = int(os.getenv("SMTP_PORT", "587"))
SMTP_USER = os.getenv("SMTP_USER", "")
SMTP_PASS = os.getenv("SMTP_PASS", "")
SMTP_FROM_NAME = os.getenv("SMTP_FROM_NAME", "PragatiVeer")
SMTP_FROM_EMAIL = os.getenv("SMTP_FROM_EMAIL", SMTP_USER)
_SMTP_CONFIGURED = bool(SMTP_HOST and SMTP_USER and SMTP_PASS)
def send_email(to: str, subject: str, html_body: str, text_body: Optional[str] = None) -> bool:
"""
Send a transactional email. Returns True on success, False on failure.
If SMTP is not configured, logs the email to console and returns True (no-op mode).
"""
if not _SMTP_CONFIGURED:
logger.warning(f"[EMAIL NO-OP] SMTP not configured. Would send to={to}, subject={subject}")
return True
try:
msg = MIMEMultipart("alternative")
msg["Subject"] = subject
msg["From"] = f"{SMTP_FROM_NAME} <{SMTP_FROM_EMAIL}>"
msg["To"] = to
if text_body:
msg.attach(MIMEText(text_body, "plain"))
msg.attach(MIMEText(html_body, "html"))
with smtplib.SMTP(SMTP_HOST, SMTP_PORT) as server:
server.ehlo()
server.starttls()
server.login(SMTP_USER, SMTP_PASS)
server.sendmail(SMTP_FROM_EMAIL, to, msg.as_string())
logger.info(f"[EMAIL SENT] to={to}, subject={subject}")
return True
except Exception as e:
logger.error(f"[EMAIL FAILED] to={to}, subject={subject}, error={e}")
return False
# --- Pre-built Email Templates ---
def send_welcome_email(to: str, name: str) -> bool:
subject = "🎉 Welcome to PragatiVeer — Your JEE/NEET Journey Starts Now!"
html_body = f"""
<div style="font-family:Arial,sans-serif;max-width:600px;margin:0 auto;background:#0a0a1a;color:#e2e8f0;padding:32px;border-radius:12px;">
<div style="text-align:center;margin-bottom:24px;">
<h1 style="color:#6366f1;font-size:28px;margin:0;">PragatiVeer</h1>
<p style="color:#94a3b8;margin:4px 0 0;">AI-Powered Exam Preparation</p>
</div>
<h2 style="color:#f1f5f9;">Welcome, {name}! 🚀</h2>
<p style="color:#cbd5e1;line-height:1.6;">
Your account has been created successfully. You now have access to:
</p>
<ul style="color:#94a3b8;line-height:2;">
<li>📚 Daily Practice Sheets (DPS)</li>
<li>📝 JEE &amp; NEET Full Mock Tests</li>
<li>📊 AI-powered performance analytics</li>
<li>🏆 Streak tracking &amp; leaderboards</li>
</ul>
<div style="text-align:center;margin:32px 0;">
<a href="https://pragativeer.com/dashboard"
style="background:#6366f1;color:#fff;padding:14px 32px;border-radius:8px;text-decoration:none;font-weight:bold;">
Go to Dashboard →
</a>
</div>
<p style="color:#64748b;font-size:12px;text-align:center;margin-top:32px;">
PragatiVeer Education Pvt. Ltd. | Jaipur, Rajasthan
</p>
</div>
"""
text_body = f"Welcome to PragatiVeer, {name}! Visit https://pragativeer.com/dashboard to get started."
return send_email(to, subject, html_body, text_body)
def send_otp_email(to: str, otp: str, purpose: str = "verification") -> bool:
subject = f"PragatiVeer: Your {purpose.capitalize()} OTP"
html_body = f"""
<div style="font-family:Arial,sans-serif;max-width:600px;margin:0 auto;background:#0a0a1a;color:#e2e8f0;padding:32px;border-radius:12px;">
<h1 style="color:#6366f1;text-align:center;">PragatiVeer</h1>
<h2 style="color:#f1f5f9;text-align:center;">Your OTP Code</h2>
<div style="background:#1e1e3a;border:2px solid #6366f1;border-radius:12px;padding:24px;text-align:center;margin:24px 0;">
<p style="color:#94a3b8;margin:0 0 8px;">Use this code to complete {purpose}:</p>
<h1 style="color:#6366f1;font-size:48px;letter-spacing:12px;margin:0;">{otp}</h1>
</div>
<p style="color:#94a3b8;text-align:center;">This code expires in <strong>10 minutes</strong>.</p>
<p style="color:#64748b;font-size:12px;text-align:center;margin-top:24px;">
If you did not request this OTP, please ignore this email.
</p>
</div>
"""
text_body = f"Your PragatiVeer {purpose} OTP is: {otp}. It expires in 10 minutes."
return send_email(to, subject, html_body, text_body)
def send_password_reset_email(to: str, reset_link: str) -> bool:
subject = "PragatiVeer: Reset Your Password"
html_body = f"""
<div style="font-family:Arial,sans-serif;max-width:600px;margin:0 auto;background:#0a0a1a;color:#e2e8f0;padding:32px;border-radius:12px;">
<h1 style="color:#6366f1;text-align:center;">PragatiVeer</h1>
<h2 style="color:#f1f5f9;">Reset Your Password</h2>
<p style="color:#cbd5e1;line-height:1.6;">
We received a request to reset the password for your PragatiVeer account.
Click the button below to set a new password:
</p>
<div style="text-align:center;margin:32px 0;">
<a href="{reset_link}"
style="background:#6366f1;color:#fff;padding:14px 32px;border-radius:8px;text-decoration:none;font-weight:bold;">
Reset Password →
</a>
</div>
<p style="color:#94a3b8;font-size:14px;">
This link expires in <strong>1 hour</strong>. If you did not request a password reset,
you can safely ignore this email.
</p>
<p style="color:#64748b;font-size:12px;text-align:center;margin-top:32px;">
PragatiVeer Education Pvt. Ltd. | Jaipur, Rajasthan
</p>
</div>
"""
text_body = f"Reset your PragatiVeer password here: {reset_link}\nThis link expires in 1 hour."
return send_email(to, subject, html_body, text_body)