Add a contact form to a Hugo site on Cloudflare Pages, with no third-party form service

Hugo builds static files, and static files can’t send email. The usual fix is a hosted form service, which means a monthly fee, a submission cap, and someone else storing your visitors’ messages.

If your site is already on Cloudflare Pages, you can skip all of that. A small Pages Function can verify the submission, build the email, and hand it to Mailjet, all inside your own Cloudflare account.

What you’ll need

  • A Hugo site deployed on Cloudflare Pages
  • A Cloudflare Turnstile widget (free) for spam protection
  • A Mailjet account with a verified sender address or domain

Step 1: Install and scaffold

npm install @d-kidd-studio/verified-form-mailer
npm exec -- verified-form-mailer init

This adds functions/api/contact.js and functions/_lib/message.js to your project. Cloudflare Pages picks up the functions folder automatically and serves the endpoint at /api/contact.

Step 2: Create your Turnstile widget

In the Cloudflare dashboard, create a Turnstile widget for your domain. You get a public site key (goes in your form) and a secret key (goes only in Cloudflare’s environment variables, never in your repo or Hugo config).

Step 3: Add the form

Drop in the form partial from the Hugo example. The Turnstile widget adds a hidden token to the form on its own, and the package verifies it server-side before anything is sent.

Step 4: Write your message builder

This is the part that makes the package flexible. The package doesn’t care what your fields are called. You decide what gets validated and what the email looks like. The Hugo example has a working starting point that strips line breaks from header fields and escapes everything that goes into the HTML body.

Step 5: Set environment variables

In your Pages project settings, add these for both Preview and Production:

  • TURNSTILE_SECRET_KEY
  • MAILJET_API_KEY
  • MAILJET_API_SECRET
  • CONTACT_FROM_EMAIL
  • CONTACT_TO_EMAIL

The from address must be a sender you’ve verified in Mailjet, or delivery will fail.

Step 6: Deploy and test

Push, submit the form once from the live site, and confirm the email arrives with a working reply-to.

Why self-host this?

You pay only for what you already use. Your submissions never sit in another company’s database. And since the handler is plain JavaScript in your own repo, you can change anything about it.

Get the package

It’s open source under the MIT license: Verified Form Mailer. If you’d rather have someone set it up and keep an eye on it, I do that for small business sites.