The form is the business
For a local insurance office, the quote form is the website's whole job. Everything else on the site exists to get a visitor to that form. The old site ran a WordPress form plugin, which did two things with every submission: it wrote the entry to the site's database, and it emailed the office. When I rebuilt the site as static pages with no server, the form needed a home, and the obvious answer, a small cloud function that sends an email, is a downgrade. One email hiccup and a prospect who believes they asked for a quote never hears back, and nobody ever knows it happened.
So the design started from the failure, not the happy path. What follows is the handler as built and tested. It runs as a function behind an API gateway, reachable only through the site's CDN, and the whole thing is about three hundred lines plus its tests.
Store first, then send
A submission is written to object storage first, encrypted at rest, as a complete JSON record. Only then is the email sent. The response the visitor sees depends on both results, and the table is the design:
| Stored | Emailed | Visitor sees |
|---|---|---|
| yes | yes | success |
| yes | no | success, because the lead is safe and the email is recoverable |
| no | yes | success, because the office has it |
| no | no | an error and the office phone number, never a false thank-you |
The last row is the one most forms get wrong. If nothing worked, the visitor must be told so while they are still on the page and still motivated, with a way to reach a human. "Thanks, we'll be in touch" over a failed request is the worst outcome in the table, and it is the default behaviour of most form code.
Reply-To is the customer
The old plugin sent each notification from the office address to the
office address, so pressing Reply sent the agent's answer back to
themselves. The new form sends from a dedicated address on a
subdomain, forms@mail. followed by the business's domain,
with Reply-To set to the customer. Reply now reaches the
person who asked. Blank fields are left out of the email, and the
subject line names the customer, so a day's notifications are
scannable in an inbox.
Why a subdomain. Mail that claims to be from the main domain but arrives from outside the business's own mail tenant is exactly the shape that Microsoft 365 anti-spoofing quarantines, and a group mailbox blocks outside senders outright by default. A subdomain with its own SPF and DKIM records sidesteps all of that, keeps the main domain's SPF a one-line record, and keeps the business's email reputation separate from anything automated. The records themselves are covered in the post on email authentication.
Bots, without a captcha
Two checks, neither visible to a person. A honeypot field that humans never see and bots fill in. And a timestamp set when the form loads: a submission completed in under three seconds was not typed by anyone. Either check failing means the submission is discarded and the bot receives a normal success response, so it learns nothing about what tripped it. No captcha, no third-party script, no tracking. The honeypot value is never written to the stored record, which one of the tests exists to prove.
The rate limit that keeps the bill flat
A public function endpoint is a pay-per-request bill that anyone on the internet can run up. Two things stop that here. First, the function's gateway only accepts requests carrying a secret header that the CDN adds on the way through, so a direct hit on the gateway is refused before the function runs. Second, a web application firewall at the CDN edge: 100 requests per five minutes per address on the form path, 2,000 per five minutes across the whole site, and an empty POST is rejected with a 400 before it goes anywhere. Above those limits the edge answers and the function never wakes up.
The firewall itself costs a few dollars a month. For a form that gets a handful of real submissions a day, the function, the gateway, the storage and the email together cost cents. Without the limit, the same stack has no ceiling.
A canary, so you find the break first
A form can break silently in a dozen ways: an expired sending identity, a rotated secret, a storage permission someone tightened. A scheduled test submission carries a token. It is stored under a separate prefix with a short retention, emailed to a monitoring address instead of the office, and marked in the subject. A forged token is treated as an ordinary submission, so the token cannot be used to probe. A broken quote form should be discovered by me, not by a customer who gave up and called a competitor.
Tests before the form existed
The field lists were transcribed from the live forms rather than guessed, which is how I found that the old site's personal-insurance page was serving the business-insurance form, and had been for some time. Thirty-odd unit tests pin the behaviour above. Their names are the specification:
test_should_store_before_emailing
test_should_still_succeed_when_email_fails_but_lead_is_stored
test_should_fail_loudly_when_both_paths_fail
test_should_set_reply_to_the_customer
test_should_encrypt_the_stored_lead
test_should_discard_when_honeypot_filled
test_should_discard_when_submitted_impossibly_fast
test_should_not_leak_honeypot_into_stored_lead
test_should_not_email_the_agency_for_a_canary
test_should_ignore_a_forged_canary_token
test_personal_form_is_not_the_business_form
Every one of them runs locally with the storage and email calls replaced by fakes, so the whole suite takes a second and needs no cloud account.
If you are building one
- Decide the four-row table first. The last row is the design.
- Store before you send, encrypted, as the complete record.
- Send from a subdomain with its own mail records. Reply-To the customer.
- Honeypot plus a timing check. Bots get a normal success and nothing stored.
- Let the function answer only to the CDN, and put a per-address limit at the edge.
- Schedule a canary that cannot be forged and does not land in the owner's inbox.
- Transcribe the real fields, then write the tests before the handler.
Where it stands
Built, tested, and running on the site's preview hostname with the firewall attached and the canary green. The public cutover of that site waits on the owner's schedule, so the production numbers are not in yet. When they are, the monthly note the business receives will include them: how many submissions, how many bots turned away, and what the whole thing cost, which should be a number small enough to be slightly embarrassing next to the old hosting bill.