I am having a problem with my contact form on my website. The website is currently live but the contact form doesn't want to send any emails whether there are errors or not.
Here is my code. Please could you tell me where I made a mistake? Any advice would be helpful.
<?php
if(isset($_POST['email'])) {
// EDIT THE 2 LINES BELOW AS REQUIRED
$email_to = "tabaccogiftshop@gmail.com";
$email_subject = $_POST['last_name'];
/*function died($error) {
// your error code can go here
echo "We are very sorry but there were errors whith your submit";
echo "These errors appear bellow. <br> <br>";
echo $error."<br> <br>";
echo "Please go back and fix those errors";
die();
}*/
// validation expected data exists
if(!isset($_POST['first_name']) || !isset($_POST['last_name']) || !isset($_POST['email']) || !isset($_POST['comments']) || !isset($_POST['captcha'])) {
$error_message = "Sva polja se moraju unijeti!";
}
$first_name = $_POST['first_name'];
$last_name = $_POST['last_name'];
$email_from = $_POST['email'];
$comments = $_POST['comments'];
$captcha = $_POST['captcha'];
$error_message = "";
$success = "";
$string_exp = "/^[A-Za-z .'-]+$/";
if(!preg_match($string_exp, $first_name)) {
$error_message = "Name should not hold numbers!";
}
if(!preg_match($string_exp, $last_name)) {
$error_message = "Subject should not hold numbers!";
}
if(!preg_match("/5/", $captcha)) {
$error_message = "Captcha is not valid, please try again!";
}
if(strlen($comments) < 2 ) {
$error_message = "Message you have entered is too short, please try again!";
}
if(strlen($error_message) > 0) {
echo '
<div class="alert alert-danger alert-dismissible">
<strong>Error! </strong>'.$error_message.'
<button class="close" data-dismiss="alert"> ×</button>
</div>
';
include_once('includes-en/form-en.php');
include_once('includes-en/footer-en.php');
die();
}
if(isset($_POST['submit']) && strlen($error_message) === 0) {
$success = "We have recieved your message, we will get in touch with you as soon as possible!";
}
if(strlen($success) > 0) {
echo '
<div class="alert alert-success alert-dismissible">
<strong>Success! </strong>'.$success.'
<button class="close" data-dismiss="alert"> ×</button>
</div>
';
}
function clean_string($string) {
$bad = array("content-type", "bcc:", "to:", "cc:", "href");
return str_replace($bad, "", $string);
}
$email_message = "Form details below. \n\n";
$email_message .= "First Name: ".clean_string($first_name)."\n";
$email_message .= "Last Name: ".clean_string($last_name)."\n";
$email_message .= "Email: ".clean_string($email_from)."\n";
$email_message .= "Message: ".clean_string($comments)."\n";
// Create email headers_list
$headers = 'From: '.$email_from.'\r\n'.'Reply To: '.$email_from.'\r\n'.'X-Mailer: PHP/' .phpversion();
@mail($email_to, $email_subject, $email_message, $headers);
?>
<?php
}
?>