2026-06-06 06:37:41 -06:00
|
|
|
"""Contact form API for indx.earth.
|
|
|
|
|
|
|
|
|
|
Required environment variables:
|
|
|
|
|
SMTP_HOST
|
|
|
|
|
SMTP_PORT
|
|
|
|
|
SMTP_USER
|
|
|
|
|
SMTP_PASSWORD
|
|
|
|
|
CONTACT_TO_EMAIL
|
|
|
|
|
CONTACT_FROM_EMAIL
|
2026-06-09 11:57:54 -06:00
|
|
|
RECAPTCHA_SECRET_KEY
|
2026-06-06 06:37:41 -06:00
|
|
|
"""
|
|
|
|
|
|
|
|
|
|
from __future__ import annotations
|
|
|
|
|
|
2026-06-09 11:57:54 -06:00
|
|
|
import json
|
2026-06-06 06:37:41 -06:00
|
|
|
import os
|
|
|
|
|
import re
|
|
|
|
|
import smtplib
|
2026-06-09 11:57:54 -06:00
|
|
|
import urllib.parse
|
|
|
|
|
import urllib.request
|
2026-06-06 06:37:41 -06:00
|
|
|
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
|
|
|
|
|
|
|
|
|
|
|
2026-06-09 11:57:54 -06:00
|
|
|
def verify_recaptcha(token: str, remote_ip: str) -> None:
|
|
|
|
|
if not token:
|
|
|
|
|
raise HTTPException(status_code=400, detail="CAPTCHA verification required")
|
|
|
|
|
|
|
|
|
|
payload = urllib.parse.urlencode(
|
|
|
|
|
{
|
|
|
|
|
"secret": require_env("RECAPTCHA_SECRET_KEY"),
|
|
|
|
|
"response": token,
|
|
|
|
|
"remoteip": remote_ip,
|
|
|
|
|
}
|
|
|
|
|
).encode("utf-8")
|
|
|
|
|
|
|
|
|
|
verify_request = urllib.request.Request(
|
|
|
|
|
"https://www.google.com/recaptcha/api/siteverify",
|
|
|
|
|
data=payload,
|
|
|
|
|
headers={"Content-Type": "application/x-www-form-urlencoded"},
|
|
|
|
|
method="POST",
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
try:
|
|
|
|
|
with urllib.request.urlopen(verify_request, timeout=10) as response:
|
|
|
|
|
result = json.loads(response.read().decode("utf-8"))
|
|
|
|
|
except Exception as exc:
|
|
|
|
|
raise HTTPException(status_code=502, detail="CAPTCHA verification failed") from exc
|
|
|
|
|
|
|
|
|
|
if not result.get("success"):
|
|
|
|
|
raise HTTPException(status_code=400, detail="Invalid CAPTCHA")
|
|
|
|
|
|
|
|
|
|
|
2026-06-06 06:37:41 -06:00
|
|
|
@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(""),
|
2026-06-09 11:57:54 -06:00
|
|
|
recaptcha_response: str = Form("", alias="g-recaptcha-response"),
|
2026-06-06 06:37:41 -06:00
|
|
|
) -> 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")
|
|
|
|
|
|
2026-06-09 11:57:54 -06:00
|
|
|
client_ip = request.client.host if request.client else "unknown"
|
|
|
|
|
verify_recaptcha(recaptcha_response, client_ip)
|
|
|
|
|
|
2026-06-06 06:37:41 -06:00
|
|
|
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")
|
|
|
|
|
|
|
|
|
|
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})
|