A Jekyll contact form that works on GitHub Pages
Jekyll turns your site into plain files, so nothing on it can send an email. Save one include that posts to FormHaven and every message reaches your inbox, spam filtered and stored in the EU, with no gem to add to your Gemfile.
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/' | relative_url }}">
<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
Save the include
Save the code above as _includes/contact-form.html with your own address in the action. Jekyll keeps folders that start with an underscore out of the built site, so only pages that include it show the form.
-
3
Put it on your contact page
Write {% include contact-form.html %} in contact.md or in a layout. The page needs front matter, even two lines of --- with nothing between them, or Jekyll leaves the tag as text.
-
4
Add a thank-you page and test
Create thanks.md with permalink: /thanks/ in its front matter. Run bundle exec jekyll serve, open http://localhost:4000, send the form and check your inbox.
A different subject line per page
Jekyll passes parameters to an include, so one file can serve your contact page and your quote page. Add a hidden _subject field that reads include.subject, and the default filter fills in a subject when a page passes none. Sort your inbox by it.
<input type="hidden" name="_subject"
value="{{ include.subject | default: 'New message from the website' }}">
<!-- on quote.md -->
{% include contact-form.html subject="Quote request" %}
On a GitHub Pages project site
A project site lives at USERNAME.github.io/REPOSITORY, so a bare /thanks/ opens on the wrong address. Set baseurl once: relative_url in the include puts it in front of /thanks/. When the site moves to your own domain, empty baseurl and the include stays as it is. FormHaven only follows _next to the site the form was sent from.
url: "https://USERNAME.github.io"
baseurl: "/REPOSITORY"
Stop bots that skip the hidden field
Turn on the bot check in the form's settings and add this line under the form in the include, so every page that shows the form loads it. Visitors see nothing, and it sets no cookies.
<script src="https://formhaven.net/bot-check.js" async></script>
Questions about this setup
- Does this work on GitHub Pages without a plugin?
- Yes. The include is HTML plus two filters every Jekyll site has, relative_url and default, so GitHub Pages builds it like any other page. Nothing runs on GitHub when a visitor presses Send: the browser posts straight to FormHaven.
- Why does my page show the include tag as text?
- The file has no front matter. Jekyll only runs Liquid in files that start with a front matter block, so add two lines of --- at the top, even with nothing between them.
- Can I put the form in a blog post?
- Yes. Posts run Liquid too, so {% include contact-form.html %} works in a file in _posts as it does on a page.
- 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.
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.