Reputation: 190
I'm trying to validate google's nocaptcha recaptcha and then send the data to an email address. I have this php code:
<?php
function mailsend(){
$name = trim(strip_tags($_POST['firstname']));
$email = trim(strip_tags($_POST['email']));
$message = htmlentities($_POST['bug']);
$subject = "Bug report submitted :(";
$to = "[email protected]";
$nametitle = "Name:";
$messagetitle = "Message:";
$body = <<<HTML
$nametitle
$name
$messagetitle
$message
HTML;
$headers = "From: $email\r\n";
$headers = "Content-type: text/html\r\n";
mail($to, $subject, $body, $headers);
}
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
// Construct the Google verification API request link.
$params = array();
$params['secret'] = 'secret_key'; // Secret key
if (!empty($_POST) && isset($_POST['g-recaptcha-response'])) {
$params['response'] = urlencode($_POST['g-recaptcha-response']);
}
$params['remoteip'] = $_SERVER['REMOTE_ADDR'];
$params_string = http_build_query($params);
$requestURL = 'https://www.google.com/recaptcha/api/siteverify?' . $params_string;
// Get cURL resource
$curl = curl_init();
// Set some options
curl_setopt_array($curl, array(
CURLOPT_RETURNTRANSFER => 1,
CURLOPT_URL => $requestURL,
));
// Send the request
$response = curl_exec($curl);
// Close request to clear up some resources
curl_close($curl);
$response = @json_decode($response, true);
if ($response["success"] == true) {
echo '<h3 class="alert alert-success">Your Bug report has been sent sucessfully!</h3>';
mailsend();
} else {
echo '<h3 class="alert alert-danger">You have not completed the recaptcha!</h3>';
}
}
?>
I'm also using this HTML Form:
<form class="col s12" action="" method="post">
<div class="row">
<div class="input-field col s6">
<input id="first_name" type="text" class="validate" name="firstname" required>
<label for="first_name">First Name</label>
</div>
<div class="input-field col s6">
<input id="last_name" type="text" class="validate" name="lastname">
<label for="last_name">Last Name</label>
</div>
</div>
<div class="row">
<div class="input-field col s12">
<input id="email" type="email" class="validate" name="email" required>
<label for="email">Email</label>
</div>
</div>
<div class="row">
<div class="input-field col s12">
<i class="mdi-editor-mode-edit prefix"></i>
<textarea id="icon_prefix2" class="materialize-textarea" name="bug" required></textarea>
<label for="icon_prefix2">Please explain your issue clearly here</label>
</div>
</div>
<div class="row">
<div class="input-field col s12">
<div class="g-recaptcha" data-sitekey="site_key"></div>
</div>
</div>
<button class="btn waves-effect waves-light" type="submit" name="action" required>Submit
<i class="mdi-content-send right"></i>
</button>
<span class='msg'><?php echo $msg; ?></span>
</form>
Currently the code successfully validates the recaptcha but then does not send the email with the data from the form to the desired email address. My Question is what is the best way to implement google's nocaptcha recaptcha and then send the data from the form to an email address. Thanks in advance for the help!
EDIT:
Have worked out how to get this working and will paste the code here for anyone else who needs help with this.
The PHP:
<?php
function mailform(){
$name = $_POST['firstname'];
$email = $_POST['email'];
$message = $_POST['bug'];
$from = 'Bug Reporting System';
$to = '[email protected]';
$subject = 'New Bug Report';
$message_formatted = "
<h3>New Bug Reported by $from</h3>
<h3>From: $name</h3>
<h5>Bug:</h5>
<p>$message</p>
";
$headers = "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type:text/html;charset=UTF-8" . "\r\n";
mail($to, $subject, $message_formatted, $headers);
}
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
// Construct the Google verification API request link.
$params = array();
$params['secret'] = 'secret_key';
if (!empty($_POST) && isset($_POST['g-recaptcha-response'])) {
$params['response'] = urlencode($_POST['g-recaptcha-response']);
}
$params['remoteip'] = $_SERVER['REMOTE_ADDR'];
$params_string = http_build_query($params);
$requestURL = 'https://www.google.com/recaptcha/api/siteverify?' . $params_string;
// Get cURL resource
$curl = curl_init();
// Set options
curl_setopt_array($curl, array(
CURLOPT_RETURNTRANSFER => 1,
CURLOPT_URL => $requestURL,
));
// Send the request
$response = curl_exec($curl);
// Close request to clear up some resources
curl_close($curl);
$response = @json_decode($response, true);
if ($response["success"] == true) {
echo '<h5 class="alert alert-success">Your Bug report has been sent sucessfully!</h5>';
mailform();
} else {
echo '<h5 class="alert alert-danger">You have not completed the recaptcha!</h5>';
}
}
?>
The origional HTML Form does not need to be changed. I'm using the Materialize framework, if you were wondering why there was a lot of markup.
Upvotes: 1
Views: 66
Reputation: 351
This is probably not the issue, but it is worth mentioning. According to your code above, it looks like you are sending the email to the hard coded [email protected]. I do not see where you set the recipient to a different address than that one.
Again, maybe you edited it to something generic so you could post it, but I am just going by what is written there.
Upvotes: 1