PHP 表单 + Google reCAPTCHA 验证邮件 html 模板

PHP form + Google reCAPTCHA validation with Mail html template

我已经使用 Php 构建了一个带有 reCaptcha 验证的表单,但我在编程方面遇到了困难 php。想设置2个参数不知道找谁建

目前,一切正常,但如果验证码失败,则会发送电子邮件。

我只是在 PHP 中遇到了编程困难。这是我想做的:

  1. 当 reCaptcha 失败时 - 在表单中显示消息对话框并且不发送电子邮件;

  2. 当 reCaptcha 成功时 - 发送邮件显示成功消息到php页。

表单部分:(调用脚本)

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

消息显示一次成功或失败的形式:

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

     $url = 'https://www.google.com/recaptcha/api/siteverify';
    $privatekey = "PRIVATE KEY";

    $response = file_get_contents($url."?secret=".$privatekey."&response=".$_POST['g-recaptcha-response']."&remoteip=".$_SERVER['REMOTE_ADDR']);
    $data = json_decode($response);

    if(isset($data->success) AND $data->success==true){

        header('Location: appel_de_service.php?CaptchaPass=True');


 $to = "email@domain.to";
 $from = $_POST['courriel'];
 $from_name = $_POST['nom_responsable'];
 $subject = "Reception d'un appel de service ";
 $nom_compagnie = $_POST['nom_compagnie']; // required
 $adresse = $_POST['adresse']; // required
 $ville = $_POST['ville'];
 $province = $_POST['province'];
 $code_postale = $_POST['code_postale'];
 $nom_responsable = $_POST['nom_responsable'];
 $courriel = $_POST['courriel'];
 $telephone = $_POST['telephone'];
 $marque = $_POST['marque'];
 $numero_modele = $_POST['numero_modele'];
 $garantie = $_POST['garantie'];
 $description = $_POST['description'];              
 $disponibilite = $_POST['disponibilite']; // required
 $coordonnees = $_POST['coordonnees']; // required

 $headers  = 'MIME-Version: 1.0' . "\r\n";
 $headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
 $headers .= "Reply-to: $courriel";

 $message = "
 <!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">
<html>
<head>
<title>Appel de service (rempli sur le site internet)</title>
<style type=\"text/css\">
h1,h2,h3,h4,h5,h6 {
    font-family: Cambria, \"Hoefler Text\", \"Liberation Serif\", Times, \"Times New Roman\", serif;
    color: #272727;
}
</style>
</head>
<body>
<h2 style=\"font-size: 1.25em; font-family: Gotham, ʼHelvetica Neueʼ, Helvetica, Arial, sans-serif;\">Vous avez re&ccedil;u une demande d'appel de service en ligne.</h2>
<table width=\"500\" border=\"1\" cellpadding=\"5\" cellspacing=\"2\">
  <tbody>
    <tr>
      <td colspan=\"2\" align=\"left\" valign=\"middle\" bgcolor=\"#84BDEC\"><h3>Informations sur l'adresse du service</h3></td>
    </tr>
    <tr>
      <td width=\"155\" align=\"left\" valign=\"middle\" bgcolor=\"#D5D5D5\">Nom de la compagnie</td>
      <td width=\"313\" align=\"left\" valign=\"middle\">$nom_compagnie</td>
    </tr>
    <tr>
      <td align=\"left\" valign=\"middle\" bgcolor=\"#D5D5D5\">Adresse</td>
      <td align=\"left\" valign=\"middle\">$adresse</td>
    </tr>
    <tr>
      <td align=\"left\" valign=\"middle\" bgcolor=\"#D5D5D5\">Ville</td>
      <td align=\"left\" valign=\"middle\">$ville</td>
    </tr>
    <tr>
      <td align=\"left\" valign=\"middle\" bgcolor=\"#D5D5D5\">Province</td>
      <td align=\"left\" valign=\"middle\">$province</td>
    </tr>
    <tr>
      <td align=\"left\" valign=\"middle\" bgcolor=\"#D5D5D5\">Code postale</td>
      <td align=\"left\" valign=\"middle\">$code_postale</td>
    </tr>
    <tr>
      <td align=\"left\" valign=\"middle\" bgcolor=\"#D5D5D5\">Nom du responsable</td>
      <td align=\"left\" valign=\"middle\">$nom_responsable</td>
    </tr>
    <tr>
      <td align=\"left\" valign=\"middle\" bgcolor=\"#D5D5D5\">T&eacute;l&eacute;phone</td>
      <td align=\"left\" valign=\"middle\">$telephone</td>
    </tr>
    <tr>
      <td align=\"left\" valign=\"middle\" bgcolor=\"#D5D5D5\">Adresse courriel</td>
      <td align=\"left\" valign=\"middle\">$courriel</td>
    </tr>
  </tbody>
</table>
<br>
<table width=\"500\" border=\"1\" cellpadding=\"5\" cellspacing=\"2\">
  <tbody>
    <tr>
      <td colspan=\"2\" align=\"left\" valign=\"middle\" bgcolor=\"#84BDEC\"><h3>Informations sur le produit</h3></td>
    </tr>
    <tr>
      <td width=\"155\" align=\"left\" valign=\"middle\" bgcolor=\"#D5D5D5\">Marque</td>
      <td width=\"313\" align=\"left\" valign=\"middle\">$marque</td>
    </tr>
    <tr>
      <td align=\"left\" valign=\"middle\" bgcolor=\"#D5D5D5\">Num&eacute;ro de mod&egrave;le</td>
      <td align=\"left\" valign=\"middle\">$numero_modele</td>
    </tr>
    <tr>
      <td align=\"left\" valign=\"middle\" bgcolor=\"#D5D5D5\">Garantie</td>
      <td align=\"left\" valign=\"middle\">$garantie</td>
    </tr>
    <tr bgcolor=\"#D5D5D5\">
      <td colspan=\"2\" align=\"left\" valign=\"middle\">Description du probl&egrave;me :</td>
    </tr>
    <tr>
      <td height=\"75\" colspan=\"2\" align=\"left\" valign=\"top\">$description</td>
    </tr>
  </tbody>
</table>
<br>
<table width=\"500\" border=\"1\" cellpadding=\"5\" cellspacing=\"2\">
  <tbody>
    <tr>
      <td colspan=\"2\" align=\"left\" valign=\"middle\" bgcolor=\"#84BDEC\"><h3>Pr&eacute;f&eacute;rences pour le rendez-vous</h3></td>
    </tr>
    <tr>
      <td width=\"155\" align=\"left\" valign=\"middle\" bgcolor=\"#D5D5D5\">Disponibilit&eacute;</td>
      <td width=\"313\" align=\"left\" valign=\"middle\">$disponibilite</td>
    </tr>
    <tr>
      <td align=\"left\" valign=\"middle\" bgcolor=\"#D5D5D5\">Coordonn&eacute;es</td>
      <td align=\"left\" valign=\"middle\">$coordonnees</td>
    </tr>
  </tbody>
</table>
</body>
</html>
 ";

 // Always set content-type when sending HTML email

 mail($to, $subject, $message, $headers);

 }

     }else{

        echo '<h2 class="text center">La vérification Anti-Spam na pas été validée. Veiullez revenir en arrière et compléter la partie Anti-Spam. </h2>';


    }


 ?> 

你快到了。您只需要将所有内容包装在 if-else 语句中。而且您也不必连同 URL 一起发送远程 IP 地址。所以表单提交后,像这样处理表单:

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

        $url = 'https://www.google.com/recaptcha/api/siteverify';
        $privatekey = "PRIVATE KEY";

        $response = file_get_contents($url."?secret=".$privatekey."&response=".$_POST['g-recaptcha-response']);
        $data = json_decode($response);

        if(isset($data->success) AND $data->success==true){
            $to = "email@domain.to";
            $from = $_POST['courriel'];
            $from_name = $_POST['nom_responsable'];


            // so on...all your mail parameters here


            if(mail($to, $subject, $message, $headers)){
                echo "mail has been successfully sent successfully";
            }else{
                echo "mail could not be sent";
            }
        }else{
            echo "Invalid captcha. Please try again";
        }

    }else{
        echo '<h2 class="text center">User has not submitted the form</h2>';
    }

 ?>