Free, simple, PHP based email contact form

This page presents the sample code for a simple ‘contact us’ form. The form collects a few pieces of information ( email, name and a message ) from your visitor and emails it to you.

Note: You can make contact forms quickly with Simfatic Forms. Simfatic Forms helps you to make complete, feature rich forms and get it online quickly. Read more here.

Get more free, ready to use form downloads at

Why to have a contact form?

“Contact us” forms are essential for almost any website. The ‘contact us’ form provides an easy interface through which your visitors can communicate to you. Your visitors can quickly submit their views, opinions and suggestions about your website , product or service.

The HTML contact form code

We will create a simple contact form with 3 fields: name, email address and a message field. Making the contact form simple (any form – for that matter) gets you more submissions. The more the number of fields, the more reluctant your visitors will be to submit the form.

The HTML code of the form is given below:

<form method="post" name="contact_form"
    Your Name:
    <input type="text" name="name">

    Email Address:
    <input type="text" name="email">

    <textarea name="message"></textarea>

    <input type="submit" value="Submit">

In the contact form download, the form code is in the email-contact-form.html page. To embed the form in a web page, just copy and paste the HTML form code to the web page.

Validating the form submission

Form validations are essential for any web form. For this simple contact form, we will make all the fields mandatory and will make sure that the email field is in the format: name@domain.tld.

It is better to do validations both on the client side and on the server side. Client side validation provides a quick feedback to your visitor. However, the client side validation can just be bypassed by disabling JavaScript in the browser. Therefore, we need to validate on the server side as well.

For client side validation, we will use the Free JavaScript Form Validation Script. The script is very simple to use and has almost all validation types built-in.

Here is the client side form validation code:

<script language="JavaScript">
var frmvalidator  = new Validator("contactform");
frmvalidator.addValidation("name","req","Please provide your name");
frmvalidator.addValidation("email","req","Please provide your email");
  "Please enter a valid email address");

Server-side processing

Once the contact form is submitted, the form submission data is sent to the script mentioned in the action attribute of the form (contact-form-handler.php in our form). The script then will collect the form submission data, validate it and send the email.

The first step is to validate the data. Ensure that the mandatory fields are filled-in, and that the email is in proper format.

The server-side code is given below:

$errors = '';
$myemail = '';//<-----Put Your email address here.
if(empty($_POST['name'])  || 
   empty($_POST['email']) || 
    $errors .= "\n Error: all fields are required";

$name = $_POST['name']; 
$email_address = $_POST['email']; 
$message = $_POST['message']; 

if (!preg_match(
    $errors .= "\n Error: Invalid email address";

Emailing the form data using PHP

We will now compose and send the email.

if( empty($errors))


$to = $myemail;

$email_subject = "Contact form submission: $name";

$email_body = "You have received a new message. ".

" Here are the details:\n Name: $name \n ".

"Email: $email_address\n Message \n $message";

$headers = "From: $myemail\n";

$headers .= "Reply-To: $email_address";


//redirect to the 'thank you' page

header('Location: contact-form-thank-you.html');


We first check whether the validations succeeded. If there were errors, the email is not sent. The PHP mail function is used to send the email. After sending the email, the visitor is redirected to the ‘thank you’ page.

Download the code for the contact form

Click here to download

The download contains the code for the HTML form, the validations and the PHP form handler.

More Contact forms:

Comments on this entry are closed.

  • hello, i did exactly what you have written and when I use wamp I complete the form and send it, it looks like the information has been sent to my shows the message thank you etc,but I have no mail in my account.

    will make sure that the email field is in the format: name@domain.tld.

    is there anything i can do about this?

    thank you, sorry about my english.

  • I made a new folder called “contact” where I placed all the files and put the ‘contact-form.html’ code on my contact.html page.

    Now whenever I submit the message, the “contact-form-handler.php” starts to download and does not send an email.

    Please help! THank you!

    • your web server does not support PHP

    • 1) { $boundary = uniqueid(“EWSEWS”); $headers.=”Content-Type:multipart/mixed”. “; boundary = $boundary\r\n\r\n”. “This is a MIME Encoded message.\r\n\r\n”. “–$boundary”; foreach($attachment as $att) { $headers.=”\r\n”. do_Attachment($att). “–$boundary”; } $headers. = “–\r\n”; } else { $headers.=”do_Attachment($attachment[0])”; }

  • I downloaded the code for the contact form

    “Click here to download” above.

    I designed a contact_us.html, contactform.php., contact.css, thank-you.php, class.phpmailer.php, fgcontactform.php, gen_validatorv31.js, and it is not working yet.

    What am I missing?

  • Thanks for giving us…

  • Hi when i click submit .. Php codes are coming in the browser . why the process is not happening???? me new to php.
    reply soon

  • I did what had to be done including downloading the source code; how do i get the message into my e-mail account just to test the code works.

  • Thank you for this helpful guide. PHP is quite hard for me to understand!

  • Whenever i click on submit button php page start downloading i am using drop box as a hosting server plz reply

  • i keep getting the following error message:
    “Error: couldnot get Form object contactform”

    this makes no sense to me. i copied everything exactly from another website i used this form on. the original site works fine, but the new site keep giving me this error

  • ok so i managed to get rid of the error message but im still not receiving the emails

    • Tim, I am receiving the same message. How did you resolve yours?

  • Thanks for the great form, is there any way I can change the colours etc of the email it sends to me?


  • Beside the webserver with php supported, Do I need a mail server to make this work?

    Son Tranduc

  • Hi,

    Thanks for great tutorials. Its work fine.
    Can you please help me for how can user receive the thank you message on there mail.


  • Hi, it works perfectly apart one thing. I added extra fields for my needs but the javascript alert (if empty) works only in the first two input fields. How is it possibile to make the alert working on all of them?
    here’s my form:
    Thanks if anyone can help!!

  • sir…
    the above code is running ”’
    but i get thank uuuu messs…
    no getting the email to the desired email addresss///

    please reply ”’
    its urgent

  • well, my comment still awaiting moderation..but there’s not need anymore. I coudn’t see a very stupid error in my third field, which was affecting the functioning of the js in all following fields. Now it’w working perfectly and I even managed to put a radiobutton!!

  • i m not getting any msg my email account .//
    i do as u tell but still not proper responce.//

  • Awsome tut, thanks worked as intended! Now I can finally finish my college project: !