pagehaus

Guides

An HTML contact form without PHP

A static site can't send email by itself: there's no server code to run. You still don't need PHP. The form posts to something that receives the message and emails it to you.

Why forms used to need PHP

An HTML form only collects what people type. Pressing Send submits it to the address in its action, and something there has to read it and do something with it. On traditional hosting that was a PHP script calling mail(). A plain HTML site has no scripts on the server, so the form needs somewhere else to post to.

Three ways to do it

  1. A host that handles forms for you. Some static hosts, including Pagehaus and Netlify, accept form submissions and email them to you. Nothing to set up beyond the HTML.
  2. A form service. Services exist that only receive forms: you point your form's action at their address. They work with any host, but you sign up for another account, and free plans usually cap messages per month.
  3. A mailto: link. <a href="mailto:you@example.com"> opens the visitor's email app. It needs no service, but many people have no email app set up, and your address is visible to spam bots.

A working example

On Pagehaus, any form that posts to /_form/ followed by a name works on your site straight away:

<form action="/_form/contact" method="post">
  <label>Your name <input name="name" required></label>
  <label>Your email <input name="email" type="email" required></label>
  <label>Message <textarea name="message" required></textarea></label>

  <!-- Where to send people afterwards -->
  <input type="hidden" name="_redirect" value="/thanks.html">
  <!-- The subject of the email you get -->
  <input type="hidden" name="_subject" value="New message from my website">
  <!-- A trap for spam bots: hidden from people, so it stays empty -->
  <input name="_gotcha" style="display:none" tabindex="-1" autocomplete="off">

  <button>Send</button>
</form>

Each message is emailed to you, with Reply-To set to the visitor's email, so you can answer straight from your inbox. Messages are also kept in your dashboard. Make a thanks.html page for the _redirect. All the details are in the contact forms guide.

Keeping spam out

Make it pleasant to use