121 lines
4.4 KiB
Python
121 lines
4.4 KiB
Python
"""
|
|
contact_lead.py — route: POST /contact-lead.
|
|
|
|
Public, product-level lead capture from the BettersightBox marketing
|
|
page's contact form — unlike every other route in this service, this one
|
|
is NOT tenant-scoped (no slug in the URL, no TenantRegistry involved):
|
|
it's about the product itself, not any individual client's Frappe site.
|
|
Reached from the marketing page over the `marketing` nginx container's
|
|
own reverse-proxy (see marketing-nginx.conf) — the browser calls
|
|
same-origin `/contact-lead`, nginx forwards it to this service over the
|
|
Docker network, so no public domain or CORS setup is needed for this
|
|
specifically.
|
|
|
|
Sends straight to Resend as a notification email to the product owner —
|
|
never stores the submitted address anywhere, never emails the submitter
|
|
back.
|
|
"""
|
|
|
|
import logging
|
|
import os
|
|
import re
|
|
|
|
import requests
|
|
import sentry_sdk
|
|
from flask import Blueprint, g, jsonify, request
|
|
|
|
logger = logging.getLogger("contact_lead")
|
|
|
|
contact_lead_bp = Blueprint("contact_lead", __name__)
|
|
|
|
# Deliberately simple — just enough to reject obvious garbage before
|
|
# calling out to Resend at all, not a full RFC 5322 validator.
|
|
_EMAIL_RE = re.compile(r"^[^@\s]+@[^@\s]+\.[^@\s]+$")
|
|
|
|
|
|
def _send_lead_notification(email, trace_id):
|
|
"""
|
|
Sends one notification email via Resend's REST API directly — no SDK
|
|
dependency, this project already uses `requests` everywhere else.
|
|
|
|
Data flow:
|
|
email (str) — the submitted address, already validated by the caller
|
|
trace_id (str) — this request's trace id, for log correlation
|
|
returns (bool) — True if Resend accepted the send, False otherwise
|
|
|
|
Steps:
|
|
1. Read RESEND_API_KEY/CONTACT_LEAD_NOTIFY_EMAIL from the
|
|
environment — if either is missing, log and give up rather than
|
|
raise, since a misconfigured notify step shouldn't 500 the form
|
|
2. POST to Resend's /emails endpoint
|
|
3. Log + report to Sentry on any failure; the caller always returns
|
|
a generic success shape to the browser regardless
|
|
"""
|
|
# Step 1
|
|
api_key = os.environ.get("RESEND_API_KEY")
|
|
notify_to = os.environ.get("CONTACT_LEAD_NOTIFY_EMAIL")
|
|
if not api_key or not notify_to:
|
|
logger.error(
|
|
"trace_id=%s RESEND_API_KEY/CONTACT_LEAD_NOTIFY_EMAIL not configured, dropping lead",
|
|
trace_id,
|
|
)
|
|
sentry_sdk.capture_message("contact_lead: Resend not configured")
|
|
return False
|
|
|
|
from_email = os.environ.get("CONTACT_LEAD_FROM_EMAIL", "onboarding@resend.dev")
|
|
|
|
# Step 2
|
|
try:
|
|
response = requests.post(
|
|
"https://api.resend.com/emails",
|
|
headers={"Authorization": f"Bearer {api_key}"},
|
|
json={
|
|
"from": from_email,
|
|
"to": [notify_to],
|
|
"subject": "New BettersightBox contact form lead",
|
|
"html": f"<p>New contact form submission from the marketing site:</p><p><b>{email}</b></p>",
|
|
},
|
|
timeout=10,
|
|
)
|
|
response.raise_for_status()
|
|
except Exception as exc:
|
|
# Step 3
|
|
logger.error("trace_id=%s failed to notify via Resend: %s", trace_id, exc)
|
|
sentry_sdk.capture_exception(exc)
|
|
return False
|
|
|
|
return True
|
|
|
|
|
|
@contact_lead_bp.route("/contact-lead", methods=["POST"])
|
|
def contact_lead():
|
|
"""
|
|
Handles a marketing-page contact form submission.
|
|
|
|
Data flow:
|
|
request.json["email"] (str) — the submitted address
|
|
returns (Flask response) — {"status": "ok"} (always, once the
|
|
email itself validates) or a 400 for
|
|
an obviously-invalid submission
|
|
|
|
Steps:
|
|
1. Reject with 400 before calling out to anything if the submitted
|
|
value doesn't look like an email at all
|
|
2. Best-effort notify via Resend — failures are logged/reported to
|
|
Sentry, never surfaced to the browser as an error; a prospective
|
|
customer's form submission shouldn't look broken because of a
|
|
transient notification-delivery problem on our end
|
|
"""
|
|
trace_id = g.trace_id
|
|
data = request.get_json(silent=True) or {}
|
|
email = (data.get("email") or "").strip()
|
|
|
|
# Step 1
|
|
if not _EMAIL_RE.match(email):
|
|
return jsonify({"error": "invalid email", "trace_id": trace_id}), 400
|
|
|
|
# Step 2
|
|
_send_lead_notification(email, trace_id)
|
|
|
|
return jsonify({"status": "ok", "trace_id": trace_id}), 200
|