Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/306.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
通过验证google reCAPTCHA使用PHP发送邮件_Php - Fatal编程技术网

通过验证google reCAPTCHA使用PHP发送邮件

通过验证google reCAPTCHA使用PHP发送邮件,php,Php,我想用PHP发送一封邮件,并通过google reCAPTCHA进行验证。我编写以下代码。看起来一切都很好。但每次出现问题时,它都会显示出错误。请检查下面的错误。请帮帮我 <?php if (isset($_POST['submit'])) { $name = trim($_POST['name']); $email = $_POST['email']; $subject = trim($_POST['subject']); $message = $_POST['message'];

我想用PHP发送一封邮件,并通过google reCAPTCHA进行验证。我编写以下代码。看起来一切都很好。但每次出现问题时,它都会显示出错误。请检查下面的错误。请帮帮我

<?php
if (isset($_POST['submit'])) {

$name = trim($_POST['name']);
$email = $_POST['email'];
$subject = trim($_POST['subject']);
$message = $_POST['message'];

// Google reCAPTCHA
require_once('recaptchalib.php');
$privatekey = "6Lcnk_USAAAAAHhVB97WBfaXq4-XN1DkCHjLO3j-";
$resp = recaptcha_check_answer ($privatekey, $_SERVER["REMOTE_ADDR"], $_POST["recaptcha_challenge_field"], $_POST["recaptcha_response_field"]);
if (!$resp->is_valid) {
    // What happens when the CAPTCHA was entered incorrectly
    $captchaErr = "The reCAPTCHA wasn't entered correctly. Please try it again.";
} else {
    // Your code here to handle a successful verification
    $validCaptcha = ture;
}

// Validate email address with PHP
if (empty($email)) {
    $emailErr = "Email is required";
}else if(!filter_var($email, FILTER_VALIDATE_EMAIL)){
    $emailErr = "Invalid email format";
}else{
    $validEmail = true;
}

// Validate name with PHP
if (!empty($name)) {
    if (!preg_match("/^[a-zA-Z ]*$/",$name)) {
        $nameErr = "Only letters and white space are allowed.";
    }
} else {
    $validName = true;
}

// Validate message with PHP
if ( strlen($message) < 15 ) {
    $messageErr = "At least 15 letters required.";
} else {
    $validMessage = true;
}

// If all validation are true than send mail
$to = 'example@yahoo.com';
$headers = 'From: $name <$email>'."\r\n";

if ( $validCaptcha && $validEmail && $validName && $validMessage ) {
    $sendMail = mail( $to, $subject, $message, $headers );
}

// Show message to user
if ( $sendMail ) {
    $sendMailSucc = '<div class="">Email has been sent successful.</div>';
} else {
    $sendMailErr = '<div class="error">Something is wrong. Please check the error bellow.</div>';
}

}
 ?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>reCAPTCHA</title>
<link rel="stylesheet" href="style.css">
</head>
<body>
<div class="container">

    <script type="text/javascript">
        var RecaptchaOptions = {
            // red, white, blackglass, clean
            theme : 'clean'
        };
    </script>

    <form action="" class="contact_form" method="POST">
        <div class="confirmation">
            <?php echo $sendMailSucc; ?><?php echo $sendMailErr; ?>
        </div>
        <div>
            <label for="">Full Name:</label>
            <input type="text" name="name" value="<?php echo $name; ?>">
            <span class="error"><?php echo $nameErr; ?></span>
        </div>
        <div>
            <label for="">Email:</label>
            <input type="text" name="email" value="<?php echo $email; ?>">
            <span class="error"><?php echo $emailErr; ?></span>
        </div>
        <div>
            <label for="">Subject:</label>
            <input type="text" name="subject" value="<?php echo $subject; ?>">
        </div>
        <div>
            <label for="message">Message:</label>
            <textarea name="message" id="" cols="30" rows="10"><?php echo $message; ?></textarea>
            <span class="error"><?php echo $messageErr; ?></span>
        </div>
        <div>
            <?php
                require_once('recaptchalib.php');
                $publickey = "6Lcnk_USAAAAADYvvrn9_CE1-HvAjke4GlcQolYE"; // you got this from the signup page
                echo recaptcha_get_html($publickey);
            ?>
            <span class="error"><?php echo $captchaErr; ?></span>
        </div>
        <div>
            <input type="submit" name="submit" value="Send">
        </div>
    </form>
</div>

Change$headers='From:$name'。\r\n;to$headers=From:$name。\r\n;在单引号中,php value not process许多有效名称包含的不仅仅是字母和空格,我找不到任何错误,但它显示出一些错误。请检查下面的错误。。有人能帮我找出代码中的错误吗?