Spaces:
Running
Running
| import httpx | |
| import logging | |
| from config.settings import get_settings | |
| logger = logging.getLogger(__name__) | |
| settings = get_settings() | |
| FROM_EMAIL = "CodeNexus <noreply@codenexus.qzz.io>" | |
| async def send_email(to: str, subject: str, html: str) -> bool: | |
| """Send email via Resend. Returns True on success.""" | |
| try: | |
| async with httpx.AsyncClient(timeout=15.0) as client: | |
| response = await client.post( | |
| "https://api.resend.com/emails", | |
| headers={"Authorization": f"Bearer {settings.resend_api_key}", "Content-Type": "application/json"}, | |
| json={"from": FROM_EMAIL, "to": [to], "subject": subject, "html": html} | |
| ) | |
| if response.status_code not in (200, 201): | |
| logger.error(f"Resend error {response.status_code}: {response.text}") | |
| return False | |
| return True | |
| except Exception as e: | |
| logger.error(f"Email send failed: {e}") | |
| return False | |
| async def send_welcome_email(to: str, name: str) -> bool: | |
| return await send_email( | |
| to=to, | |
| subject="Welcome to CodeNexus! π", | |
| html=f""" | |
| <div style="font-family:Inter,sans-serif;max-width:600px;margin:0 auto;background:#030712;color:#fff;padding:40px;border-radius:16px;"> | |
| <h1 style="color:#6366f1;font-size:28px;margin-bottom:8px;">Welcome to CodeNexus!</h1> | |
| <p style="color:#9ca3af;font-size:16px;">Hi {name}, your account is ready.</p> | |
| <p style="color:#d1d5db;">Start writing and analyzing code right now β it's completely free.</p> | |
| <a href="https://codenexus.qzz.io/editor" style="display:inline-block;margin-top:24px;padding:14px 28px;background:#6366f1;color:#fff;border-radius:10px;text-decoration:none;font-weight:600;"> | |
| Open Editor β | |
| </a> | |
| <p style="color:#4b5563;font-size:12px;margin-top:32px;">CodeNexus β Where Code Meets Intelligence</p> | |
| </div> | |
| """ | |
| ) | |
| async def send_support_notification_to_admin(admin_email: str, user_email: str, category: str, message: str) -> bool: | |
| return await send_email( | |
| to=admin_email, | |
| subject=f"[CodeNexus Support] New {category} ticket from {user_email}", | |
| html=f""" | |
| <div style="font-family:Inter,sans-serif;max-width:600px;margin:0 auto;background:#030712;color:#fff;padding:40px;border-radius:16px;"> | |
| <h2 style="color:#f59e0b;">New Support Ticket</h2> | |
| <p><strong>From:</strong> {user_email}</p> | |
| <p><strong>Category:</strong> {category}</p> | |
| <p><strong>Message:</strong></p> | |
| <div style="background:#111827;padding:16px;border-radius:8px;color:#d1d5db;">{message}</div> | |
| <a href="https://codenexus.qzz.io/admin" style="display:inline-block;margin-top:24px;padding:14px 28px;background:#6366f1;color:#fff;border-radius:10px;text-decoration:none;font-weight:600;"> | |
| View in Admin Panel β | |
| </a> | |
| </div> | |
| """ | |
| ) | |
| async def send_support_confirmation_to_user(to: str, category: str) -> bool: | |
| return await send_email( | |
| to=to, | |
| subject="We received your support request β CodeNexus", | |
| html=f""" | |
| <div style="font-family:Inter,sans-serif;max-width:600px;margin:0 auto;background:#030712;color:#fff;padding:40px;border-radius:16px;"> | |
| <h2 style="color:#6366f1;">We got your message!</h2> | |
| <p style="color:#9ca3af;">Thanks for reaching out about <strong>{category}</strong>.</p> | |
| <p style="color:#d1d5db;">Our team will review your request and get back to you as soon as possible.</p> | |
| <p style="color:#4b5563;font-size:12px;margin-top:32px;">CodeNexus β Where Code Meets Intelligence</p> | |
| </div> | |
| """ | |
| ) | |
| async def send_ticket_reply_to_user(to: str, reply: str) -> bool: | |
| return await send_email( | |
| to=to, | |
| subject="Reply to your CodeNexus support ticket", | |
| html=f""" | |
| <div style="font-family:Inter,sans-serif;max-width:600px;margin:0 auto;background:#030712;color:#fff;padding:40px;border-radius:16px;"> | |
| <h2 style="color:#6366f1;">Support Reply</h2> | |
| <div style="background:#111827;padding:16px;border-radius:8px;color:#d1d5db;">{reply}</div> | |
| <p style="color:#4b5563;font-size:12px;margin-top:32px;">CodeNexus Support Team</p> | |
| </div> | |
| """ | |
| ) | |