I am trying to make a contact form using HTML and PHP. The PHP for the form is below:
<?php
if(isset($_POST['email'])) {
// Email to information
$email_to ="personalemail@email.com";
$email_subject ="Contact";
$email_from ="Person";
// Error code
function died($error) {
echo "We are sorry, but there were error(s) found with the form you submitted.";
echo "These errors appear below.<br/><br/>";
echo $error. "<br/><br/>";
echo "Please go back and fix these errors.<br/>";
die();
}
// Validation
if(!isset($_POST['fname']) || !isset($_POST['lname']) || !isset($_POST['email']) || !isset($_POST['message'])) {
died('We are sorry but there appears to be a problem with the form you submitted.');
}
$fname = $_POST['fname'];
$lname = $_POST['lname'];
$email = $_POST['email'];
$message = $_POST['message'];
$error_message = "";
if(!filter_var($email, FILTER_VALIDATE_EMAIL)) {
$error_message .= 'The email address you entered does not appear to be valid.<br/>';
}
$string_exp = "/^[A-Za-z.'-]+$/";
if(!preg_match($string_exp, $fname)) {
$error_message .= 'The first name you entered does not appear to be valid.<br/>';
}
if(!preg_match($string_exp, $lname)) {
$error_message .= 'The last name you entered does not appear to be valid.<br/>';
}
if(strlen($message) < 2) {
$error_message .= 'The message you entered does not appear to be valid.<br/>';
}
if(strlen($error_message) > 0) {
died($error_message);
}
$email_message = "Form details below.
";
function clean_string($string) {
$bad = array("content-type", "bcc:", "to:", "cc:", "href");
return str_replace($bad, "", $string);
}
$email_message .= "Name:" . clean_string($fname) . clean_string($lname) . "
";
$email_message .= "Email:" . clean_string($email) . "
";
$email_message .= "Message:" . clean_string($message) . "
";
// Create email headers
$headers = 'From: ' .$email_From . "
". 'Reply-To:' . $email. "
" . 'X-Mailer: PHP/' . phpversion();
mail($email_to, $email_subject, $email_message, $headers);
?>
Thankyou for contacting me. I will be in contact with you shortly. <br/>
Please click <a href="index.html">here</a> to go back to the main website
<?php
}
?>
The issue is that when the form is submitted, it never sends an email to my own personal email. Is there something more I have to set up with the web hosting or is the problem purely in the code? I did some research about the appropriate code to put in and have tried many different options but none seem to work.