Free PHP Contact Form
In my quest to create a php contact form, I have run into many problems. With that in mind I am posting below the HTML and the PHP needed for a simple php contact form. This form has the added benefit of working on Apache being hosted with Godaddy, the worst hosting service ever, but the cheapest.
Good Luck!
| Contact us using this form: | |
| * Name: | |
| * Email: | |
| Phone: | |
| Message: | |
| A * indicates a field is required | |
HTML:

PHP: (be sure to save this page as contact.php in the same folder as the html page above)
$to = "insert the email addess you want the form to be submitted to here";
$from = $_REQUEST['Email'] ;
$name = $_REQUEST['Name'] ;
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/plain;'."\r\n";
$headers .= 'From: Sales-Mailer <insert the email addess you want the form to be submitted to here>'."\r\n";
$subject = "Web Contact Data";
$fields = array();
$fields{"Name"} = "Name";
$fields{"Email"} = "Email";
$fields{"Phone"} = "Phone";
$fields{"Message"} = "Message";
$body = "We have received the following information:\n\n";
foreach($fields as $a => $b){ $body .= sprintf("%20s: %s\n",$b,$_REQUEST[$a]); }
$headers2 = $headers; // you don't really need to set all the data again for a second var, since it's the same values
$subject2 = "Thank you for contacting us";
$autoreply = "Thank you for contacting us. Somebody will get back to you as soon as possible, usualy within 1 hour. If you have any more questions, please consult our website at www.yoursitename.com";
if($from == '') { print "You have not entered an email, please go back and try again"; }
elseif($name == '') { print "You have not entered a name, please go back and try again"; }
else {
$send = mail($to, $subject, $body, $headers, "-f insert the email addess you want the form to be submitted to here");
$send2 = mail($from, $subject2, $autoreply, $headers2);
if($send)
{
header("Location: http://www.yoursite.com/thankyou.html" );
exit; //whenever you do a re-direct, use exit, or else it will keep trying to process your code many times
}
else
{
print "We encountered an error sending your mail, please notify youremailaddress.com";
}
}
?>
Comments
Post a Comment