I have been working on a form in a php file. As you'll see from the code below I have written everything in a PHP file including the form tag. This form.php file is being called from another source in my folder called contact.php. I'll admit this is the neatest written code. But for some reason when I submit my information on this form, it tells me that the form has been submitted. Yet I do not receive an email ($email_to) to my billy.farroll@hotmail email address with any of the submitted information. It also works with the google recaptcha and if there is anything invalid entered. Just for some reason it will not send through the data to me i.e. Name, Email, Contact Number or the Enquiry.
Help would be GREATLY appreciated.
My Inline CSS style:
<style>
form span{
color: red;
}
form input[type="submit"]{
background: url('images/logo2.png') no-repeat;
background-size: contain;
overflow: visible;
padding-left: 45px;
height: 20px;
border: 0px;
margin: 0 auto;
margin-top: 5px;
font-family: 'Orbitron', sans-serif;
text-transform: uppercase;
}
.g-recaptcha {
margin: 5px;
}
</style>
My PHP
<?php
if(strpos($_SERVER['REQUEST_URI'], 'form' )!== false){
header('Location:index.php');
}
if($_POST){
$email;
$name;
$captcha;
$telephone;
$question;
if(isset($_POST['email']))
$email=$_POST['email'];
if(isset($_POST['name']))
$name=$_POST['name'];
if(isset($_POST['telephone']))
$telephone=$_POST['telephone'];
if(isset($_POST['message']))
$question=$_POST['message'];
if(isset($_POST['g-recaptcha-response']))
$captcha=$_POST['g-recaptcha-response'];
$email = htmlspecialchars($_POST['email']);
if (!preg_match("/([\w\-]+\@[\w\-]+\.[\w\-]+)/", $email) || ($name == "") ||
($question == "") ||(!$captcha))
{
echo "<p>Invalid Input.</p>";
?>
<form action=<?php echo htmlspecialchars($_SERVER["PHP_SELF"]); ?>
method="post" style="text-align:left;">
<label>Name: <span>*</span></label><input name="name" type="text" />
<label>Email: <span>*</span></label><input name="email" type="text" />
<label>Contact Number: </label><input name="telephone" type="text" />
<label>Enquiry: <span>*</span></label>
<textarea name="message" rows="10" cols="30"></textarea>
<div class="g-recaptcha" data-sitekey="<!-- SITEKEY -->"></div>
<input type="submit" value="submit" />
</form>
<?php
} else {
$response=json_decode(file_get_contents("https://www.google.com/recaptcha/api/siteverify?secret=<!-- SECRET KEY -->=".$captcha."&remoteip=".$_SERVER['REMOTE_ADDR']), true);
if($response['success'] == false)
{
echo '<h2>Spam detected</h2>';
}
else {
$email_from = $_POST["email"];
$email_to = "billy.farroll@hotmail.com";
$question = $_POST["message"];
$email_subject = "Enquiry";
$headers =
"From: $email_from
";
"Reply-To: $email_from
";
$message =
"Name: ". $name .
"
Mobile Number: " . $telephone .
"
Email Address: " . $email .
"
" .
"
Message:
" . $question;
ini_set("sendmail_from", $email_from);
$sent = mail($email_to, $email_subject, $message, $headers, "-f" .$email);
if ($sent)
{
echo 'Thank you for your enquiry, one of our team will be in contact with you shortly.';
}
}
}
} else {
?>
<form action=<?php echo htmlspecialchars($_SERVER["PHP_SELF"]); ?> method="post" style="text-align:left;">
<label>Name: <span>*</span></label><input name="name" type="text" />
<label>Email: <span>*</span></label><input name="email" type="text" />
<label>Contact Number: </label><input name="telephone" type="text" />
<label>Enquiry: <span>*</span></label>
<textarea name="message" rows="10" cols="30"></textarea>
<div class="g-recaptcha" data-sitekey="<!-- SITEKEY -->"></div>
<input type="submit" value="submit" /><span> * required fields</span>
</form>
<?php } ?>