首页 > 解决方案 > PHPMailer + reCAPTCHA:发送失败

问题描述

我正在尝试制作联系表格,我在网上搜索了很多,但没有找到任何有效的解决方案。

这是联系表单 HTML 和 PHP:这是我的代码,面临错误提交失败。提交表格时

<form method="POST" action="<?php $_SERVER['PHP_SELF']; ?>">
                                <p>
                                    <input type="text" name="name" id="news_signup_name" autofocus placeholder="Enter your name" required>
                                </p>
                                <p>
                                    <input type="email" name="email" id="news_signup_email" placeholder="Enter your email" required>
                                </p>
                                <p>
                                    <input type="text" name="website" id="news_signup_website" placeholder="Enter your website" required>
                                </p>
                                <p>
                                    <input type="text" name="designation" id="news_signup_designation" placeholder="Enter your designation" required>
                                </p>
                                <p>
                                    <input type="number" name="conatct" id="news_signup_contact_number" placeholder="Enter your conatct no." required>
                                </p>
                                <div class="g-recaptcha" data-sitekey="XXXXXXXXXXXXXXXXXXXXXXXXXXXXX">
                                </div>
                                <p class="button">
                                    <input name="submit" type="submit" value="Submit">
                                </p>
                          </form>
<?php

    require ("class.phpmailer.php");
    
    $name = "";
    $email = "";
    $website = "";
    $designation = "";
    $conatct = "";

    //Get submitted data
    if(isset($_POST['submit'])){

        $response = $_POST["g-recaptcha-response"];
        $url = 'https://www.google.com/recaptcha/api/siteverify';
        $data = array(
            'secret' => 'XXXXXXXXXXXXXXXXXXXXXXXXXXXX',
            '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);

        //Check that captcha is valid or not
        if ($captcha_success->success==false) {

              // Message if mail has been sent
                 echo "<script>
                    alert('Please try agian and enter valid captcha.');
                </script>";
        } 
        else if ($captcha_success->success==true) {

            $name=$_POST['name']; 
            $email=$_POST['email'];  
            $website=$_POST['website'];  
            $designation=$_POST['designation']; 
            $conatct=$_POST['conatct']; 

            if($name == "" ||
                $email == "" ||
                $website == "" ||
                $designation == "" ||
                $conatct == ""){
                    echo "<script>
                         alert('All fields are required. Please fill up all fields.');
                     </script>";
                }
            else{
                $mail = new PHPMailer();
             
                $mail->IsSMTP();
                $mail->Host = "mail.test.com"; // Your Domain Name
                 
                $mail->SMTPAuth = true;
                $mail->SMTPSecure = 'ssl'; // secure transfer enabled REQUIRED for Gmail
                $mail->Port = 465;
                $mail->Username = "donotreply@test.com"; // Your Email ID
                $mail->Password = "XXXXXX"; // Password of your email id
                 
                $mail->From = "donotreply@test.com";
                $mail->FromName = "tesst";
                $mail->AddAddress ("info@test.com"); // On which email id you want to get the message
                $mail->AddCC ($email);
                 
                $mail->IsHTML(true);
                 
                $mail->Subject = "Enquiry from Website submitted by $name"; // This is your subject
                 
                // HTML Message Starts here
                 
                $mail->Body = "
                <html>
                    <body>
                        <table style='width:600px;'>
                            <tbody>
                                <tr>
                                    <td style='width:150px'><strong>Name: </strong></td>
                                    <td style='width:400px'>$name</td>
                                </tr>
                                <tr>
                                    <td style='width:150px'><strong>Email: </strong></td>
                                    <td style='width:400px'>$email</td>
                                </tr>
                                <tr>
                                    <td style='width:150px'><strong>Website: </strong></td>
                                    <td style='width:400px'>$website</td>
                                </tr>
                                <tr>
                                    <td style='width:150px'><strong>Designation: </strong></td>
                                    <td style='width:400px'>$designation</td>
                                </tr>
                                <tr>
                                    <td style='width:150px'><strong>Conatct: </strong></td>
                                    <td style='width:400px'>$conatct</td>
                                </tr>
                            </tbody>
                        </table>
                    </body>
                </html>
                ";
    
                // HTML Message Ends here
                 
                if(!$mail->Send()) {
                    // Message if mail has been sent
                    echo "<script>
                        alert('Submission failed.');
                    </script>";
                }
                else {
                    // Message if mail has been not sent
                    echo "<script>
                        alert('Email has been sent successfully.');
                    </script>";
                }
            }

            
        }

    }
?>

我正在尝试使用 PHPMailer 将数据输入发送到联系表单到另一个电子邮件地址。我遵循了 Build A PHP Contact Form 但我似乎无法让它工作。这是我的 index.php

标签: phpmailersendmail

解决方案


推荐阅读