added
This commit is contained in:
commit
38ae828d82
85 files changed
+11990
No files matched your search
@@ -0,0 +1,99 @@
|
||||
"""Contact form API for indx.earth.
|
||||
|
||||
Required environment variables:
|
||||
SMTP_HOST
|
||||
SMTP_PORT
|
||||
SMTP_USER
|
||||
SMTP_PASSWORD
|
||||
CONTACT_TO_EMAIL
|
||||
CONTACT_FROM_EMAIL
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import os
|
||||
import re
|
||||
import smtplib
|
||||
from email.message import EmailMessage
|
||||
|
||||
from fastapi import FastAPI, Form, HTTPException, Request
|
||||
from fastapi.responses import JSONResponse
|
||||
|
||||
|
||||
app = FastAPI(title="iNDX Contact API")
|
||||
|
||||
EMAIL_RE = re.compile(r"^[^@\s]+@[^@\s]+\.[^@\s]+$")
|
||||
|
||||
|
||||
def require_env(name: str) -> str:
|
||||
value = os.getenv(name)
|
||||
if not value:
|
||||
raise RuntimeError(f"Missing required environment variable: {name}")
|
||||
return value
|
||||
|
||||
|
||||
@app.get("/api/contact/healthz")
|
||||
def healthz() -> dict[str, str]:
|
||||
return {"status": "ok"}
|
||||
|
||||
|
||||
@app.post("/api/contact")
|
||||
def send_contact(
|
||||
request: Request,
|
||||
name: str = Form(...),
|
||||
email: str = Form(...),
|
||||
message: str = Form(...),
|
||||
company: str = Form(""),
|
||||
) -> JSONResponse:
|
||||
if company:
|
||||
return JSONResponse({"ok": True})
|
||||
|
||||
name = name.strip()
|
||||
email = email.strip()
|
||||
message = message.strip()
|
||||
|
||||
if not name or len(name) > 120:
|
||||
raise HTTPException(status_code=400, detail="Invalid name")
|
||||
if not EMAIL_RE.match(email) or len(email) > 254:
|
||||
raise HTTPException(status_code=400, detail="Invalid email")
|
||||
if not message or len(message) > 5000:
|
||||
raise HTTPException(status_code=400, detail="Invalid message")
|
||||
|
||||
smtp_host = require_env("SMTP_HOST")
|
||||
smtp_port = int(os.getenv("SMTP_PORT", "587"))
|
||||
smtp_user = require_env("SMTP_USER")
|
||||
smtp_password = require_env("SMTP_PASSWORD")
|
||||
contact_to = require_env("CONTACT_TO_EMAIL")
|
||||
contact_from = require_env("CONTACT_FROM_EMAIL")
|
||||
|
||||
client_ip = request.client.host if request.client else "unknown"
|
||||
|
||||
email_message = EmailMessage()
|
||||
email_message["Subject"] = f"iNDX Earth contact form: {name}"
|
||||
email_message["From"] = contact_from
|
||||
email_message["To"] = contact_to
|
||||
email_message["Reply-To"] = email
|
||||
email_message.set_content(
|
||||
"\n".join(
|
||||
[
|
||||
"New contact form submission from indx.earth",
|
||||
"",
|
||||
f"Name: {name}",
|
||||
f"Email: {email}",
|
||||
f"Client IP: {client_ip}",
|
||||
"",
|
||||
"Message:",
|
||||
message,
|
||||
]
|
||||
)
|
||||
)
|
||||
|
||||
try:
|
||||
with smtplib.SMTP(smtp_host, smtp_port, timeout=20) as smtp:
|
||||
smtp.starttls()
|
||||
smtp.login(smtp_user, smtp_password)
|
||||
smtp.send_message(email_message)
|
||||
except Exception as exc:
|
||||
raise HTTPException(status_code=502, detail="Email delivery failed") from exc
|
||||
|
||||
return JSONResponse({"ok": True})
|
||||
Reference in new issue
Block a user