|
| 1 | +"""Email notification routes.""" |
| 2 | + |
| 3 | +import os |
| 4 | +import resend |
| 5 | +from fastapi import APIRouter, status |
| 6 | +from pydantic import BaseModel, EmailStr |
| 7 | + |
| 8 | +from app.utils.make_meta import make_meta |
| 9 | +from app.utils.email_templates import goldlabel_email |
| 10 | + |
| 11 | +resend.api_key = os.environ.get("RESEND_API_KEY") |
| 12 | +RESEND_API_KEY = resend.api_key |
| 13 | + |
| 14 | +router = APIRouter(prefix="/notify") |
| 15 | + |
| 16 | +class EmailRequest(BaseModel): |
| 17 | + to: EmailStr |
| 18 | + subject: str |
| 19 | + html: str |
| 20 | + cta_label: str | None = None |
| 21 | + cta_url: str | None = None |
| 22 | + |
| 23 | + |
| 24 | +def send_email_resend(to: str, subject: str, html: str) -> dict: |
| 25 | + if not resend.api_key: |
| 26 | + return {"error": "Missing RESEND_API_KEY"} |
| 27 | + params: resend.Emails.SendParams = { |
| 28 | + "from": "NX° <nx@goldlabel.pro>", |
| 29 | + "to": [to], |
| 30 | + "subject": subject, |
| 31 | + "html": html, |
| 32 | + } |
| 33 | + try: |
| 34 | + email: resend.Emails.SendResponse = resend.Emails.send(params) |
| 35 | + return dict(email) |
| 36 | + except Exception as e: |
| 37 | + return {"error": str(e)} |
| 38 | + |
| 39 | + |
| 40 | +@router.get("/email") |
| 41 | +def root() -> dict: |
| 42 | + """GET /notify/email endpoint.""" |
| 43 | + if not RESEND_API_KEY: |
| 44 | + meta = make_meta("error", "RESEND_API_KEY is missing from environment. Please set it in your .env file.") |
| 45 | + return {"meta": meta} |
| 46 | + meta = make_meta("success", "GET /notify/email endpoint") |
| 47 | + return { |
| 48 | + "meta": meta, |
| 49 | + "data": { |
| 50 | + "hint": "Use POST /notify/email to send an email via Resend API.", |
| 51 | + "type": { |
| 52 | + "to": { |
| 53 | + "type": "string", |
| 54 | + "format": "email", |
| 55 | + "required": True, |
| 56 | + "description": "Recipient email address." |
| 57 | + }, |
| 58 | + "subject": { |
| 59 | + "type": "string", |
| 60 | + "required": True, |
| 61 | + "description": "Subject of the email." |
| 62 | + }, |
| 63 | + "html": { |
| 64 | + "type": "string", |
| 65 | + "required": True, |
| 66 | + "description": "HTML content of the email." |
| 67 | + }, |
| 68 | + "cta_label": { |
| 69 | + "type": "string", |
| 70 | + "required": False, |
| 71 | + "description": "Optional CTA button label. Defaults to 'Call To Action'." |
| 72 | + }, |
| 73 | + "cta_url": { |
| 74 | + "type": "string", |
| 75 | + "required": False, |
| 76 | + "description": "Optional CTA URL. Defaults to the website base URL." |
| 77 | + } |
| 78 | + } |
| 79 | + } |
| 80 | + } |
| 81 | + |
| 82 | + |
| 83 | +@router.post("/email", status_code=status.HTTP_202_ACCEPTED) |
| 84 | +def send_email(request: EmailRequest): |
| 85 | + """POST /notify/email endpoint to send email via Resend API.""" |
| 86 | + if not RESEND_API_KEY: |
| 87 | + meta = make_meta("error", "RESEND_API_KEY missing. Please set it in your .env file.") |
| 88 | + return {"meta": meta} |
| 89 | + |
| 90 | + result = send_email_resend( |
| 91 | + to=request.to, |
| 92 | + subject=request.subject, |
| 93 | + html=goldlabel_email( |
| 94 | + request.subject, |
| 95 | + request.html, |
| 96 | + cta_label=request.cta_label or "Call To Action", |
| 97 | + cta_url=request.cta_url or "https://goldlabel.pro", |
| 98 | + ), |
| 99 | + ) |
| 100 | + if "error" in result: |
| 101 | + meta = make_meta("error", result["error"]) |
| 102 | + return {"meta": meta} |
| 103 | + |
| 104 | + meta = make_meta("success", "Email sent successfully.") |
| 105 | + return {"meta": meta, "data": result} |
0 commit comments