PHP + SMTP 表单 + reCaptcha

Bak*_*rom 2 php phpmailer recaptcha

我正在将 Google reCaptcha 添加到这个 PHPMailer 表单中。

它应该通过 SMTP 发送。

我应该如何验证 reCaptcha 并发送当前表格?if语句应该怎么写在这里?

这是我的 index.html 文件的代码:

<form class="form-subscribe" action="mail.php" method="post">
<div class="container">
<div class="row">
<input type="text" class="form-control col-md-5 form-name" name="name" placeholder="???? ???" required>
<div class="col-md-1"></div>
<input type="text" class="form-control col-md-6 form-email" name="email" placeholder="???? ??. ?????" required>
<textarea type="text" class="form-control form-text bg-gray col-md-12" name="message" placeholder="???? ?????????" required></textarea>
</div>
<div class="input-group-append">
<div class="g-recaptcha" data-sitekey=""></div>
<button class="button button-shadow2" type="submit" name="submit">?????????</button>
</div>
</div>
</form>
Run Code Online (Sandbox Code Playgroud)

这是mail.php文件的代码:

    <?php 

    require_once('phpmailer/PHPMailerAutoload.php');
    $mail = new PHPMailer;
    $mail->CharSet = 'utf-8';

    $name = $_POST['name'];
    $email = $_POST['email'];
    $message = $_POST['message'];

    $mail->isSMTP();
    $mail->Host = 'smtp.gmail.com';
    $mail->SMTPAuth = true;
    $mail->Username = '';
    $mail->Password = '';
    $mail->SMTPSecure = 'ssl';
    $mail->Port = 465;

    $mail->setFrom($email); 
    $mail->addAddress('');
    $mail->isHTML(true);                             

    $mail->Subject = '????????? ? ?????';
    $mail->Body    = '' .$name . ' ??????? ??????, ??? ??????? ' .$email. '<br>????????? ????? ??????????: ' .$message;
    $mail->AltBody = '';



 if(!$mail->send()) {
        echo 'Message sent!';
    } else {
        echo 'Error!';
    }
    ?>
Run Code Online (Sandbox Code Playgroud)

小智 10

它并不像看起来那么复杂,我在你的问题的评论中给了你一个例子。现在我给你介绍一下。

注意:这个例子没有经过测试,但我已经进行了语法检查。

在此处输入图片说明

您的 html 代码没问题,因此无需更改。

正如您已经知道的那样,您只需添加密钥:

<div class="g-recaptcha" data-sitekey="YOUR_KEY"></div>
Run Code Online (Sandbox Code Playgroud)

现在我们将获得“recaptcha 数据”:

$response = $_POST["g-recaptcha-response"];
$url = 'https://www.google.com/recaptcha/api/siteverify';
Run Code Online (Sandbox Code Playgroud)

Recptcha 需要一些数据来验证验证码:

$data = array(
    'secret' => 'YOUR_SECRET',
    'response' => $_POST["g-recaptcha-response"]
);



$options = array(
    'http' => array (
        'method' => 'POST',
        'content' => http_build_query($data)
    )
);
Run Code Online (Sandbox Code Playgroud)

接下来,脚本必须获取所有数据并验证验证码,file_get_contents它将加载重新验证码数据并使用 json 对其进行解码:

  $context  = stream_context_create($options);
  $verify = file_get_contents($url, false, $context);
  $captcha_success=json_decode($verify);
Run Code Online (Sandbox Code Playgroud)

至少我们需要一个 if 循环来检查验证码是否有效:

if ($captcha_success->success==false) {
    echo "false";
} else if ($captcha_success->success==true) {
    echo "true";
}
Run Code Online (Sandbox Code Playgroud)

下一步是快速而肮脏的:

if ($captcha_success->success==false) {
    echo "Captcha wrong";
} else if ($captcha_success->success==true) {

if(!$mail->send()) {
    echo 'Message sent!';
} else {
    echo 'Error!';
}
}
Run Code Online (Sandbox Code Playgroud)

所以完整的代码看起来像这样:

require_once('phpmailer/PHPMailerAutoload.php');
$mail = new PHPMailer;
$mail->CharSet = 'utf-8';

$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];

$mail->isSMTP();
$mail->Host = 'smtp.gmail.com';
$mail->SMTPAuth = true;
$mail->Username = '';
$mail->Password = '';
$mail->SMTPSecure = 'ssl';
$mail->Port = 465;

$mail->setFrom($email); 
$mail->addAddress('');
$mail->isHTML(true);                             

$mail->Subject = '????????? ? ?????';
$mail->Body    = '' .$name . ' ??????? ??????, ??? ??????? ' .$email. '<br>????????? ????? ??????????: ' .$message;
$mail->AltBody = '';

$response = $_POST["g-recaptcha-response"];
$url = 'https://www.google.com/recaptcha/api/siteverify';
$data = array(
    'secret' => 'YOUR_SECRET',
    'response' => $_POST["g-recaptcha-response"]
);
$options = array(
    'http' => array (
        'method' => 'POST',
        'content' => http_build_query($data)
    )
);
$context  = stream_context_create($options);
$verify = file_get_contents($url, false, $context);
$captcha_success=json_decode($verify);

 if ($captcha_success->success==false) {
    echo "Captcha wrong";
} else if ($captcha_success->success==true) {

if(!$mail->send()) {
    echo 'Message sent!';
} else {
    echo 'Error!';
}
}
Run Code Online (Sandbox Code Playgroud)

这是原始教程(json):https : //www.kaplankomputing.com/blog/tutorials/recaptcha-php-demo-tutorial/

此页面也提供了 ajax 教程:https : //www.kaplankomputing.com/blog/tutorials/php/setting-recaptcha-2-0-ajax-demotutorial/