Published on : 05.05.2010 Category : PHP Classes Viewed : 63 times.
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.
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.
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']."rnrnSender IP: ".$_SERVER["REMOTE_ADDR"]; $headers = 'From: '.$_POST['email']."rn". 'Reply-To: '.$_POST['email']."rn"; 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.