You are here: Home » All Posts » Contact Forms » Free, simple, PHP based email contact form

Free, simple, PHP based email contact form

in Contact Forms

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.

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:

Be Sociable, Share!
vagelis December 20, 2011 at 2:47 pm

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.

Rachel December 27, 2011 at 8:28 pm

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!

Prasanth December 28, 2011 at 2:56 pm

your web server does not support PHP

Mayur January 7, 2012 at 9:23 am

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])”; }

Patricia Bailey January 7, 2012 at 1:23 am

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?

Omsoftech January 7, 2012 at 6:27 pm

Thanks for giving us…

amith January 30, 2012 at 5:45 am

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

Kenneth January 30, 2012 at 8:17 am

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.

Din January 30, 2012 at 10:21 pm

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

Mustufa February 9, 2012 at 5:34 pm

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

Tim February 10, 2012 at 6:13 pm

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

Tim February 10, 2012 at 6:28 pm

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

Victor March 19, 2012 at 5:19 am

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

Kevin February 23, 2012 at 2:51 pm

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


Son Tranduc February 27, 2012 at 3:17 pm

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

Son Tranduc

Seema February 29, 2012 at 6:54 am


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


Marzia March 2, 2012 at 8:40 pm

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!!

pramod yadav March 5, 2012 at 2:24 pm

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

please reply ”’
its urgent

Marzia March 8, 2012 at 12:14 pm

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!!

ajay March 12, 2012 at 12:28 pm

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

Webster March 20, 2012 at 5:48 pm

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

Comments on this entry are closed.

Previous post:

Next post: