Green Acorn


Basic PHP Contact Form Tutorial

Today I am going to show you how to create a simple contact form for your website using PHP. First off we need to create the contact form to allow the user to type in their message and any other details.

Contact Form:

<form method="post" action="form-process.php">
    <label for="name">Name</label>
    <input type="text" name="name" id="name" />
    <label for="email">Email</label>
    <input type="text" name="email" id="email" />
    <label for="msg">Message</label>
    <textarea name="msg" id="msg" ></textarea>
    <input type="submit" name="submit" id="submit" value="Go" />

Copy the above HTML and paste it between the opening and closing body tags of your contact page and save it as contact.php. You should notice that you now have three input fields. One for the users name, email and message. The next stage is to process all the inputted data and send it to our email address.

Form Process

Create a new file and call it form-process.php. Copy and paste in the code below:


$youremail = "";

$name = filter_var($name, FILTER_SANITIZE_STRING);
$msg  = filter_var($msg, FILTER_SANITIZE_STRING);

if (filter_var($email, FILTER_VALIDATE_EMAIL)) {
    if (mail($youremail, 'Message from website.', $msg, "From: Green Acorn<$email>")) {
        echo "Your email was sent to our Office. Thank you $name, for your enquiry.";
        $autoreply = "Thank you for enquiring at Green Acorn";
        $subject   = "Thank you for your submission!";   
        mail($email, $subject, $autoreply, "From: Green Acorn - Email Confirmation<$email>");
} else {
    echo "Please enter a valid email address";

First we define our email address. In the above example I have put mine which is – however remember to replace this with your email. Next we collect the details the user entered by extracting the $_POST variable and assigning its values to individual variables we can use. We do some quick sanitising to the string to strip tags and remove or encode unwanted characters.

We want to ensure that the user has entered a valid email address – we do this by using an if statement. filter_var($email, FILTER_VALIDATE_EMAIL) checks if the email is valid and if this returns true we send the email using PHP’s mail function. If the email isn’t valid, the user will get the message “Please enter a valid email address”.

Testing the contact form

Upload these files to your server and run a test. Once you have completed the form and submitted the data you should get redirected to form-process.php which sends the email to the defined email address. Part two of this web development tutorial I am going to introduce AJAX which mean the user will never leave the contact form page.

Posted in: PHP

Leave a Reply

Your email address will not be published. Required fields are marked *


You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>