I've got everything about this form working except if the message is not sent the user isn't diverted to the "message not sent" page.
Everything works fine if the user completes the form properly - the message is sent and received and the user is diverted to the "message sent" confirmation page.
So I'm doing something wrong with the else part of the message verification.
Can anyone see what I'm doing wrong please?
<?php
if(isset($_POST['submit'])):
if(isset($_POST['g-recaptcha-response']) && !empty($_POST['g-recaptcha-response'])):
//your site secret key
$secret = 'Secret key goes here';
//get verify response data
$verifyResponse = file_get_contents('https://www.google.com/recaptcha/api/siteverify?secret='.$secret.'&response='.$_POST['g-recaptcha-response']);
$responseData = json_decode($verifyResponse);
$name = !empty($_POST['name'])?$_POST['name']:'';
$email = !empty($_POST['email'])?$_POST['email']:'';
$message = !empty($_POST['message'])?$_POST['message']:'';
if($responseData->success):
//contact form submission code
$to = 'me@myplace.com';
$subject = 'Website Enquiry';
$htmlContent = "
<h1>Website Booking Request</h1>
<p><b>Name: </b>".$name."</p>
<p><b>Email: </b>".$email."</p>
<p><b>Message: </b>".$message."</p>
";
// Always set content-type when sending HTML email
$headers = "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type:text/html;charset=UTF-8" . "\r\n";
// More headers
$headers .= 'From:'.$name.' <'.$email.'>' . "\r\n";
//send email
@mail($to,$subject,$htmlContent,$headers);
print "<meta http-equiv=\"refresh\" content=\"0;URL=messagesent.html\">";
$name = '';
$email = '';
$message = '';
else:
print " <meta http-equiv=\"refresh\" content=\"0;URL=messagenotsent.html\">";
?>