# File: backend/email_utils.py import httpx from pydantic import EmailStr import random import string import os # --- CONFIGURATION --- # Securely pull both keys from Hugging Face Settings MAILJET_API_KEY = os.getenv("MAILJET_API_KEY") MAILJET_SECRET_KEY = os.getenv("MAILJET_SECRET_KEY") # This MUST exactly match the email you registered in Mailjet SENDER_EMAIL = "docusortofficial@gmail.com" SENDER_NAME = "DocuSort" def generate_otp(length=6): return ''.join(random.choices(string.digits, k=length)) async def send_otp_email(email: EmailStr, otp: str, subject="DocuSort Verification"): # Fallback safety: If API keys are missing, print to logs instead of crashing if not MAILJET_API_KEY or not MAILJET_SECRET_KEY: print(f"\n{'='*40}") print(f" ⚠️ [MOCK EMAIL - NO API KEY] To: {email}") print(f" ⚠️ [MOCK EMAIL - NO API KEY] CODE: {otp}") print(f"{'='*40}\n") return url = "https://api.mailjet.com/v3.1/send" payload = { "Messages": [ { "From": {"Email": SENDER_EMAIL, "Name": SENDER_NAME}, "To": [{"Email": email}], "Subject": subject, "HTMLPart": f"

Your verification code is: {otp}

This code expires in 10 minutes.

" } ] } async with httpx.AsyncClient() as client: try: response = await client.post( url, json=payload, auth=(MAILJET_API_KEY, MAILJET_SECRET_KEY) ) response.raise_for_status() except Exception as e: print(f"Mailjet API Error: {e}") async def send_alert_email(email: EmailStr, subject: str, body_content: str): if not MAILJET_API_KEY or not MAILJET_SECRET_KEY: clean_body = body_content.replace("
", "\n").replace("", "").replace("", "") print(f"\n{'='*40}") print(f" ⚠️ [MOCK ALERT - NO API KEY] To: {email}") print(f" ⚠️ [MOCK ALERT - NO API KEY] Body: \n{clean_body}") print(f"{'='*40}\n") return url = "https://api.mailjet.com/v3.1/send" html_body = f"""

DocuSort Notification

{body_content}

This is an automated message from the DocuSort platform. Please do not reply directly to this email.
""" payload = { "Messages": [ { "From": {"Email": SENDER_EMAIL, "Name": SENDER_NAME}, "To": [{"Email": email}], "Subject": subject, "HTMLPart": html_body } ] } async with httpx.AsyncClient() as client: try: response = await client.post( url, json=payload, auth=(MAILJET_API_KEY, MAILJET_SECRET_KEY) ) response.raise_for_status() except Exception as e: print(f"Mailjet API Error: {e}")