A Simple PHP Contact Form Script

Update: Other than using the code outlined on this page, you may also want to download the Kavoir Contact Form script to be readily usable on your website.

It’s also a free one and a rather easy one too. You should be able to do it yourself. To create a working contact form, you need the front end and the back end. The front end presents the contact form to the website user and the back end accepts the data sent from the form by the user and take any necessary actions such as relaying the message to your email.

Visit FormKid.com for hosted free contact form solution.

Front end (HTML)

Simply copy this HTML form and paste it into your web pages where you want it to be, such as on the sidebar so a visitor can send you a contact message on any page.

<form action="/cf.php" method="post">
	<p><label for="input-email">Your Email: </label><br/><input type="text" name="email" id="input-email" size="40"/></p>
	<p><label for="input-message">Your Message: </label><br/><textarea type="text" name="message" id="input-message" rows="10" cols="40"></textarea></p>
	<p><button type="submit" name="submit">Send</button></p>

You may also want to style this form a little bit to make it look fancier. But make sure you don’t change any of the properties inside the tags such as action=”…” and name=”…”.

Back end (PHP)

Copy and paste these PHP code into a file named cf.php, change [email protected] to your own email address that you wish to receive the messages. Put the file at the root directory of your domain so that everyone can access it at http://www.yoursite.com/cf.php.


if (isset($_GET['success'])) {

	?><p><span style="color:green;">Message successfully sent.</span> Thank you!</p><?php

} else {

	if (!empty($_POST['email']) && !empty($_POST['message'])) {

		$to = '[email protected]'; // your email address, can be @gmail.com, etc.
		$subject = 'Contact from yoursite.com'; // change yoursite.com to your own domain name
		$message = $_POST['message']."\r\n\r\nSender IP: ".$_SERVER["REMOTE_ADDR"];
		$headers = 'From: '.$_POST['email']."\r\n".
					'Reply-To: '.$_POST['email']."\r\n";
		mail($to, $subject, $message, $headers);

		header("Location: /cf.php?success"); // redirects the sender to success page so he or she doesn't accidentally send multiple identical messages

	} else {

		?><p><span style="color:red;">Error detected.</span> Please make sure you have filled all fields. Press back button to go back.</p><?php




Should be functioning properly. Thus far this contact form script has been successfully tested on Rackspace Cloud, DreamHost and Linode.

There are a lot more that can be done to create a sophisticated contact form. For starters, you can implement a spam catcher and a file upload control that sends the user uploaded file as attachment to your email. You could also have on-page error detection but that’d need a lot more coding.

7 thoughts on “A Simple PHP Contact Form Script”

  1. This works perfectly on my site on Dreamhost.
    It’s very simple to setup and easy to modify…
    thank you very much for this.

  2. Nice script! I got it working quickly and easily! Do I need to be concerned about spammers having access to my email address? Is it hidden at all?

  3. Hey, just wanted to say thanks for the Contact Form code. I was having some trouble with a site I just uploaded to Dreamhost, the contact form was not working. Yours works like a dream! Thanks again!

Comments are closed.

Scroll to Top