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