A Hugo contact form that works on a static site
Save one partial and every message reaches your inbox, while your Hugo site stays plain built files. Messages are stored in the EU, and spam is filtered out before it reaches you.
14 days free, no card. Paste the address where the code says YOUR_FORM_ID.
<form action="https://formhaven.net/f/YOUR_FORM_ID" method="POST">
<label>Your name <input type="text" name="name" required></label>
<label>Your email <input type="email" name="email" required></label>
<label>Message <textarea name="message" required></textarea></label>
<input type="text" name="_gotcha" style="display:none" tabindex="-1" autocomplete="off">
<input type="hidden" name="_next" value="{{ "thanks/" | absURL }}">
<button type="submit">Send message</button>
</form>
Set it up
-
1
Create a form in FormHaven
Sign up, press New form and copy its address. It looks like https://formhaven.net/f/YOUR_FORM_ID.
-
2
Add the partial
Save the code above as layouts/_partials/contact-form.html with your own address in the action. One file, so every page that shows the form uses the same address.
-
3
Call it from a layout
Put {{ partial "contact-form.html" . }} where the form belongs in your contact page's layout.
-
4
Add a thank-you page and test
Create content/thanks.md. Run hugo server, fill in the form and check your inbox.
Put the form inside a Markdown page
Partials only run in layouts. To drop the form into content/contact.md, save a shortcode that calls the partial, then write {{< contact-form >}} where the form should appear.
{{ partial "contact-form.html" .Page }}
Stop bots that skip the hidden field
Turn on the bot check in the form's settings and add this line once, under the form in the partial. Visitors see nothing, and it sets no cookies.
<script src="https://formhaven.net/bot-check.js" async></script>
Questions about this setup
- My theme uses layouts/partials. Does that matter?
- Hugo 0.146 renamed the folders to layouts/_partials and layouts/_shortcodes. On an older Hugo, or a theme still on the old layout, save the same files in layouts/partials and layouts/shortcodes instead.
- Which hosts does this work on?
- Any host that serves the public folder Hugo builds: Netlify, Cloudflare Pages, GitHub Pages or your own server. The form posts to FormHaven, so nothing runs on the host when a visitor presses Send.
- How do I answer a message?
- Press Reply in the email. The address the visitor typed in the email field is set as the reply address.
- Can my client get the messages instead of me?
- Yes. Add up to 5 more addresses to the form. Each one confirms by a link first, and their copy carries no FormHaven branding. You can turn your own copy off.
- Where is the data stored?
- On our servers in the EU, for 1 to 365 days as you choose per form, or not at all: then each message is emailed and its answers deleted. A data processing agreement covers every account.
More to read
Get your form address in a minute
14 days free, no card. Then €1 per form a month, €0.50 from the 11th form.