A Simple PHP Contact Form Script

by Yang Yang on December 10, 2009

in Free PHP Classes & Library, PHP Tips & Tutorials

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

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>
</form>

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 youremail@yoursite.com 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.

<?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 = 'youremail@yoursite.com'; // 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.

Related Posts

{ 4 comments… read them below or add one }

Yang Yang February 26, 2010 at 6:22 pm

Any feedback on this one, guys?

Reply

Joe the FTP Hosting Tech March 18, 2010 at 8:34 pm

A real nice script we use for ftp hosting and forum hosting questions that hides the recipient from spammers is here: http://developerkarma.com/projects

enjoy

Reply

Jorge March 20, 2010 at 2:14 am

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

Reply

Yang Yang March 20, 2010 at 10:07 am

Hey no problem, Jorge, glad it helped. :)

Reply

Leave a Comment

Previous post:

Next post: