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
- 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.
- A form service. Services exist that only receive forms: you point your form's
actionat their address. They work with any host, but you sign up for another account, and free plans usually cap messages per month. - 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
- A honeypot field like
_gotchaabove catches most bots: people never see it, bots fill it in, and those messages are dropped. - Required fields and
type="email"stop empty or malformed messages before they're sent. - Don't ask for what you don't need. Never collect passwords or card numbers in a contact form. Pagehaus refuses forms that try.
Make it pleasant to use
- Give every field a
<label>, so screen readers and taps on the label both work. - Use
autocomplete="name"andautocomplete="email"so browsers can fill them in. - Say on the thank-you page when you'll reply.