60 lines
1.9 KiB
Python
60 lines
1.9 KiB
Python
import logging
|
|
from email.mime.multipart import MIMEMultipart
|
|
from email.mime.text import MIMEText
|
|
|
|
import aiosmtplib
|
|
|
|
from app.config import settings
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
|
async def send_email(to: str, subject: str, html_body: str) -> bool:
|
|
"""Send HTML email via SMTP. Returns True on success."""
|
|
if not settings.smtp_host:
|
|
logger.info(f"SMTP not configured, skipping email to {to}: {subject}")
|
|
return False
|
|
|
|
msg = MIMEMultipart("alternative")
|
|
msg["From"] = f"{settings.smtp_from_name} <{settings.smtp_from_email}>"
|
|
msg["To"] = to
|
|
msg["Subject"] = subject
|
|
msg.attach(MIMEText(html_body, "html"))
|
|
|
|
try:
|
|
await aiosmtplib.send(
|
|
msg,
|
|
hostname=settings.smtp_host,
|
|
port=settings.smtp_port,
|
|
username=settings.smtp_user,
|
|
password=settings.smtp_password,
|
|
use_tls=True,
|
|
)
|
|
return True
|
|
except Exception as e:
|
|
logger.error(f"Failed to send email to {to}: {e}")
|
|
return False
|
|
|
|
|
|
async def send_registration_notification(
|
|
admin_email: str, student_name: str, class_name: str
|
|
):
|
|
html = f"""
|
|
<h2>New Registration Pending Approval</h2>
|
|
<p><strong>{student_name}</strong> has registered for <strong>{class_name}</strong>.</p>
|
|
<p>Please log in to ClassHub to review and approve.</p>
|
|
"""
|
|
await send_email(admin_email, "ClassHub: New Registration", html)
|
|
|
|
|
|
async def send_approval_notification(student_email: str, approved: bool):
|
|
status_text = "approved" if approved else "rejected"
|
|
html = f"""
|
|
<h2>Registration {status_text.capitalize()}</h2>
|
|
<p>Your registration has been <strong>{status_text}</strong>.</p>
|
|
{"<p>You can now log in to ClassHub.</p>" if approved else ""}
|
|
"""
|
|
await send_email(
|
|
student_email, f"ClassHub: Registration {status_text.capitalize()}", html
|
|
)
|