How to add a contact form to a Hugo site (shortcode included)

Hugo builds static HTML, so forms need an endpoint. Add a reusable contact form shortcode to your Hugo site, keep the key in your site config, and receive submissions by email and Telegram.

· 3 min read

Hugo shortcode for a contact form

Hugo is one of the fastest static site generators around, and it outputs plain HTML files. That is great for performance and hosting costs, but a static file cannot process a form. You need somewhere to send the submission.

In this guide we will build a reusable contact form shortcode that posts to a form backend. You will be able to drop {{< contact-form >}} into any Markdown page.

1. Get an access key

Create a free Formiz account, copy your access key and add your email under Channels → Email. Add your domain under Settings → Allowed domains.

2. Store the key in your config

In hugo.toml:

toml
[params]
  formizKey = "fmz_your_key_here"
  contactThanks = "/thanks/"

(The key ends up in your public HTML anyway; that is fine because it can only send submissions to you.)

3. Create the shortcode

Create layouts/shortcodes/contact-form.html:

go-html-template
<form class="contact-form" action="https://api.formiz.app/submit" method="POST">
  <input type="hidden" name="access_key" value="{{ site.Params.formizKey }}">
  <input type="hidden" name="subject" value="{{ .Get "subject" | default "New message from the website" }}">
  <input type="hidden" name="redirect" value="{{ site.Params.contactThanks | absURL }}">
  <input type="checkbox" name="_honey" style="display:none" tabindex="-1" autocomplete="off">

  <label for="cf-name">Name</label>
  <input id="cf-name" name="name" autocomplete="name" required>

  <label for="cf-email">Email</label>
  <input id="cf-email" name="email" type="email" autocomplete="email" required>

  <label for="cf-message">Message</label>
  <textarea id="cf-message" name="message" rows="6" required></textarea>

  <button type="submit">Send message</button>
</form>

4. Use it in a page

In content/contact.md:

markdown
---
title: "Contact"
---

Questions, ideas or project enquiries? Send us a message.

{{< contact-form subject="New enquiry from the contact page" >}}

Then create content/thanks.md with a short thank-you message. To keep it out of your sitemap, add sitemap: { disable: true } to its front matter (Hugo 0.125 and later).

5. Build and test

Run hugo server, submit the form and check your inbox. In production, deploy as usual to Netlify, Cloudflare Pages, GitHub Pages or any static host.

Your form posts to one URL; Formiz handles spam, storage and notifications
Your form posts to one URL; Formiz handles spam, storage and notifications

Styling

Most Hugo themes style form elements already. If yours does not, a few lines in your custom CSS go a long way:

css
.contact-form { display: grid; gap: .5rem; max-width: 34rem; }
.contact-form input, .contact-form textarea { padding: .7rem .8rem; border: 1px solid #ccc; border-radius: 8px; font: inherit; }
.contact-form button { justify-self: start; padding: .7rem 1.2rem; border-radius: 8px; border: 0; background: #111; color: #fff; cursor: pointer; }

Extras

  • Multiple forms: pass a different subject to each shortcode call so you can tell them apart in your inbox.
  • More fields: any input with a name is delivered, so add phone or company to the shortcode as needed.
  • Phone notifications: connect Telegram for instant alerts.
  • Stay on the page: submit with fetch instead of redirecting. The static site guide has a ten-line script.

Get your form working in five minutes

Create a free Formiz key, add it to your form and receive submissions by email and Telegram. No server, no SMTP, no card.