Skip to main content

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.

<!DOCTYPE html>
    <meta charset="utf-8">
    <title>Your simple Contact Form</title>
   <form method="post" action="sendmail.php" name="contactform">
       <input type="input" id="fname" name="fname" />
       <label for="fname">First Name</label>
       <input type="input" id="lname" name="lname" />
       <label for="lname">Last Name</label>
       <input type="input" id="email" name="email" />
       <label for="email">Email</label>
       <input type="input" id="phone" name="phone" />
       <label for="phone">Telephone</label>
       <textarea id="message" name="message" rows="6"></textarea>
       <label for="message">Message</label>
       <input type="submit" id="submit" name="submit" value="Submit Form" />
<script src="validate.js"></script>

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

$errors = ‘’;
$fname = $_POST[‘fname’]; 
$lname = $_POST[‘lname’]; 
$email_address = $_POST[‘email’]; 
$phone = $_POST[‘phone’]; 
$message = $_POST[‘message’];
$myemail = ‘’; // Your email address goes here
if(empty($_POST[‘fname’]) || 
 empty($_POST[‘lname’]) || 
 empty($_POST[‘email’]) || 
 empty($_POST[‘phone’]) || 
echo ‘There was an issue with your form submission, please try again.’; 
if (!preg_match(
echo ‘Your email address is not valid.’;
if( empty($errors))
$to = $myemail;
$email_subject = “Contact form submission from $fname $lname”;
$email_body = “You have received a new message. “.
“ Here are the details:\n Name: $fname $lname \n “.
“Email: $email_address\n Phone: $phone\n Message \n $message”;
$headers = “From: $myemail\n”;
$headers .= “Reply-To: $email_address”;
//redirect to the ‘thank you’ page
header(‘Location: thank-you.html’);

The javascript form validator is rather simple, but effective.

var frmvalidator = new Validator("contactform");

frmvalidator.addValidation("fname", "req", "Please provide your first name");
frmvalidator.addValidation("lname", "req", "Please provide your last name");
frmvalidator.addValidation("email", "req", "Please provide your email");
frmvalidator.addValidation("phone", "req", "Please provide your phone number");
frmvalidator.addValidation("email", "email", "Please enter a valid email address");

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

<!DOCTYPE html>
    <meta charset="utf-8">
    <title>Thank you for contacting me</title>
  <h1>Thank you</h1>
  <p>Your contact form submission was successful!</p>
  <p><a href="index.html">Click here</a> to return to the form</p>

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

Leave a Reply