Hi, I’m stuck making my contact form work?
I’ve built the code, but the contact form when Submit is selected, doesn’t send an email to my default email address?
How do I do this.
I use w3schools, but that’s too basic and I’m actually surprised they’re teaching such a simple code, as I think it could encourage spamming?
http://www.w3schools.com/php/php_mail.asp
Here’s my html code:
sk8 GC | Contact UsHere’s my php code:
<?php if(isset($_POST['contactSubject'])) { // EDIT THE 2 LINES BELOW AS REQUIRED $contactSubject_to = "[email protected]"; $contactSubject_subject = "Sk8parks.org.au contact us form submitted by a sk8parks.org.au user."; function died($error) { // your error code can go here echo "We are very sorry, however there were error(s) found with the form you submitted. "; echo "These errors appear below."; echo $error."
"; echo "Please go back and fix these errors.
"; die(); } // validation expected data exists if(!isset($_POST['contactName']) || !isset($_POST['contactcontactSubject']) || !isset($_POST['contactSubject']) || !isset($_POST['contactImportance']) || !isset($_POST['contactDate'])) { died('We are sorry, however there appears to be a problem with the form you submitted.'); } $contactName = $_POST['contactName']; // required $contactcontactSubject = $_POST['contactcontactSubject']; // required $contactSubject_from = $_POST['contactSubject']; // required $contactImportance = $_POST['contactImportance']; // not required $contactDate = $_POST['contactDate']; // required $error_message = ""; $contactSubject_exp = '/^[A-Za-z0-9._%-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,4}$/'; if(!preg_match($contactSubject_exp,$contactSubject_from)) { $error_message .= 'The Subject you entered does not appear to be valid.
'; } $string_exp = "/^[A-Za-z .'-]+$/"; if(!preg_match($string_exp,$contactName)) { $error_message .= 'The Name you entered does not appear to be valid.
'; } if(!preg_match($string_exp,$contactcontactSubject)) { $error_message .= 'The Subject you entered does not appear to be valid.
'; } if(strlen($contactDate) < 2) { $error_message .= 'The Date you entered do not appear to be valid.
'; } if(strlen($error_message) > 0) { died($error_message); } $contactSubject_message = "Form details below.\n\n"; function clean_string($string) { $bad = array("content-type","bcc:","to:","cc:","href"); return str_replace($bad,"",$string); } $contactSubject_message .= "First Name: ".clean_string($contactName)."\n"; $contactSubject_message .= "Last Name: ".clean_string($contactcontactSubject)."\n"; $contactSubject_message .= "contactSubject: ".clean_string($contactSubject_from)."\n"; $contactSubject_message .= "contactImportance: ".clean_string($contactImportance)."\n"; $contactSubject_message .= "contactDate: ".clean_string($contactDate)."\n"; // create contactSubject headers $headers = 'From: '.$contactSubject_from."\r\n". 'Reply-To: '.$contactSubject_from."\r\n" . 'X-Mailer: PHP/' . phpversion(); @mail($contactSubject_to, $contactSubject_subject, $contactSubject_message, $headers); ?>
Your contact has been successful. Thank you for contacting sk8parks.org.au.
// Reference: http://www.freecontactform.com/email_form.php, viewed 201207111901.
<?php } ?>Unfortunately, the form failed to send. We may have made an error. Please contact the forum admin.