reCAPTCHA和PHP发送电子邮件表格 [英] reCAPTCHA and PHP to send an Email Form

查看:41
本文介绍了reCAPTCHA和PHP发送电子邮件表格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用GoDaddy作为我的Web服务器.

I'm using GoDaddy as my web server.

我正在尝试将reCAPTCHA实施到我的网站的电子邮件表单中,但是似乎无法正常工作.也就是说,我通过了reCAPTCHA测试,但未发送电子邮件.

I'm trying to implement reCAPTCHA into my email form for my website, however it seems to not be able to work. That is, I pass the reCAPTCHA test but the email isn't sent.

到目前为止,我只在网站上使用过PHP和HTML.

I've only used PHP and HTML for the site so far.

我也将脚本添加到了我的头部标签

I have also added the script to my head tag

<script src='https://www.google.com/recaptcha/api.js'></script>

这是代码段.

HTML:

<form action="#" id="form" method="post" name="form">
      <div class="form-group">
             <label for="name-field">Name</label>
             <input name="vname" class="form-control" placeholder="Enter Your Full Name" type="text" value="">
      </div>
      <div class="form-group">
             <label for="email-field">Email address</label>
             <input name="vemail" class="form-control" placeholder="Enter Your Email" type="text" value="">
      </div>
      <div class="form-group">
             <label for="contract-field">Enter Contract Here</label>
             <textarea name="msg" class="form-control" placeholder="Enter Contract Here" rows="5"></textarea>
      </div>
      <div class="g-recaptcha form-group" data-sitekey="SITEKEY"></div>
      <div class="form-group"><button id="send" name="submit" type="submit" class="btn btn-default">Submit</button></div>
</form>

PHP:

<?php
//Checking For reCAPTCHA
$captcha;
if(isset($_POST['g-recaptcha-response'])){
    $captcha=$_POST['g-recaptcha-response'];
}
// Checking For correct reCAPTCHA
$response=file_get_contents("https://www.google.com/recaptcha/api/siteverify?secret=SECRETKEY&response=".$captcha."&remoteip=".$_SERVER['REMOTE_ADDR']);
if($response.success==false){
    echo "Your CAPTCHA response was wrong."
    exit;
}
else{
    // Checking For Blank Fields..
    if($_POST["vname"]==""||$_POST["vemail"]==""||$_POST["msg"]==""){
        echo "Fill All Fields..";
    }
    else{
        // Check if the "Sender's Email" input field is filled out
        $email=$_POST['vemail'];
        // Sanitize E-mail Address
        $email =filter_var($email, FILTER_SANITIZE_EMAIL);
        // Validate E-mail Address
        $email= filter_var($email, FILTER_VALIDATE_EMAIL);
        if (!$email){
            echo "Invalid Sender's Email";
        }
        else{
            $to = 'johdoe@example.com';
            $subject = 'Test';
            $message = $_POST['msg'];
            $headers = 'From:'. $email . "\r\n"; // Sender's Email
            // Message lines should not exceed 70 characters (PHP rule), so wrap it
            $message = wordwrap($message, 70, "\r\n");
            // Send Mail By PHP Mail Function
            mail($to, $subject, $message, $headers);
            echo "Your mail has been sent successfully!";
        }
    }
} else {
    echo "Your CAPTCHA response was wrong.  Try again."
    exit;
}?>

我在服务器端实现错误吗?还是客户端?

Am I implementing server-side wrong? Or is it client side?

推荐答案

您的代码在if和else中存在语法错误.只需按照下面的代码重写并尝试即可.

Your code has syntax errors in if and else. Just rewrite your code as given below and try it.

<?php
//Checking For reCAPTCHA
$captcha;
if (isset($_POST['g-recaptcha-response'])) {
    $captcha = $_POST['g-recaptcha-response'];
}
// Checking For correct reCAPTCHA
$response = file_get_contents("https://www.google.com/recaptcha/api/siteverify?secret=SECRETKEY&response=" . $captcha . "&remoteip=" . $_SERVER['REMOTE_ADDR']);
if (!$captcha || $response.success == false) {
    echo "Your CAPTCHA response was wrong.";
    exit ;
} else {
    // Checking For Blank Fields..
    if ($_POST["vname"] == "" || $_POST["vemail"] == "" || $_POST["msg"] == "") {
        echo "Fill All Fields..";
    } else {
        // Check if the "Sender's Email" input field is filled out
        $email = $_POST['vemail'];
        // Sanitize E-mail Address
        $email = filter_var($email, FILTER_SANITIZE_EMAIL);
        // Validate E-mail Address
        $email = filter_var($email, FILTER_VALIDATE_EMAIL);
        if (!$email) {
            echo "Invalid Sender's Email";
        } else {
            $to = 'johdoe@example.com';
            $subject = 'Test';
            $message = $_POST['msg'];
            $headers = 'From:' . $email . "\r\n";
            // Sender's Email
            // Message lines should not exceed 70 characters (PHP rule), so wrap it
            $message = wordwrap($message, 70, "\r\n");
            // Send Mail By PHP Mail Function
            if (mail($to, $subject, $message, $headers)) {
                echo "Your mail has been sent successfully!";
            } else {
                echo "Failed to send email, try again.";
                exit ;
            }
        }
    }
}
?>

这篇关于reCAPTCHA和PHP发送电子邮件表格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆