Simple Validated Mail Form with HTML, PHP & Javascript

Have you ever wanted to create a PHP mail form on your website? Even if you have no idea what you’re doing, this guide will help you get your form up and running in no time.

You’re going to need to work from 4 files:

  1. HTML file, which holds the actual form.
  2. PHP file, which will compile the form contents, validate and actually send the email.
  3. Javascript file, which will help validate the form fields and keep spam bots from flooding your inbox.
  4. Another HTML file, which will serve as a thank you page when the form submission is completed successfully.

We are going to be creating a simple form, featuring a First Name, Last Name, Email, Phone and Message inputs. We will also be directing the post of the form to our PHP file, sendmail.php, which will take care of the rest of the processing.

Next, we will be creating the PHP sendmail.php script.

The javascript form validator is rather simple, but effective.

Lastly, the thank you page for successful submission of the form.

That’s it! Feel free to modify this script as you see necessary.