我正在尝试验证google的nocaptcha recaptcha,然后将数据发送到电子邮件地址。 我有这个PHP代码:
<?php
function mailsend(){
$name = trim(strip_tags($_POST['firstname']));
$email = trim(strip_tags($_POST['email']));
$message = htmlentities($_POST['bug']);
$subject = "Bug report submitted :(";
$to = "someone@example.com";
$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>';
}
}
?>
我也在使用这个HTML表单:
<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>
目前,代码成功验证了recaptcha,但后来却没有将包含表单数据的电子邮件发送到所需的电子邮件地址。我的问题是实施谷歌的nocaptcha recaptcha然后将表格中的数据发送到电子邮件地址的最佳方式。在此先感谢您的帮助!
修改 已经弄清楚如何使这个工作,并将这里的代码粘贴给其他需要帮助的人 PHP:
<?php
function mailform(){
$name = $_POST['firstname'];
$email = $_POST['email'];
$message = $_POST['bug'];
$from = 'Bug Reporting System';
$to = 'someone@example.com';
$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>';
}
}
?>
不需要更改原始HTML表单。我正在使用Materialize框架,如果你想知道为什么会有很多标记。
答案 0 :(得分:1)
这可能不是问题,但值得一提。根据您上面的代码,您似乎将电子邮件发送到硬编码的someone@example.com。我没有看到您将收件人设置为与该地址不同的地址。
再一次,也许你把它编辑成通用的东西,所以你可以发布它,但我只是按照那里的内容去做。