<?php
if(isset($_POST['submit'])):
  if(isset($_POST['g-recaptcha-response']) && !empty($_POST['g-recaptcha-response'])):
    // chiave segreta reCAPTCHA
    $secret = 'xxxxxxxxxxxxxxxxxxx';
    // invio della verifica del reCAPTCHA e risposta
    $verifyResponse = file_get_contents('https://www.google.com/recaptcha/api/siteverify?secret='.$secret.'&response='.$_POST['g-recaptcha-response']);
    $responseData = json_decode($verifyResponse);
      $name = !empty($_POST['name'])?$_POST['name']:'';
      $email = !empty($_POST['email'])?$_POST['email']:'';
      $phone = !empty($_POST['phone'])?$_POST['phone']:'';
      $people = !empty($_POST['people'])?$_POST['people']:'';
      $calendarstart = !empty($_POST['calendarstart'])?$_POST['calendarstart']:'';
      $calendarend = !empty($_POST['calendarend'])?$_POST['calendarend']:'';
      $message = !empty($_POST['message'])?$_POST['message']:'';
      $checkbox = !empty($_POST['checkbox'])?$_POST['checkbox']:'no';
      if($responseData->success):
        // corpo della mail
        $to = 'info@xxxxxxxxxx.it'; // <--- inserire e-mail destinatario
        $subject = "Ti ha scritto dal tuo sito:  $name";
        $htmlContent = "
        <ul style='list-style: none; padding-left: 0;'>
        <li style='margin: 0 0 15px 0;'>Questi sono i dettagli del messaggio:</li>
        <li style='margin: 0 0 5px 0;'><b>Nome:</b> ".$name."</li>
        <li style='margin: 0 0 5px 0;'><b>E-mail:</b> ".$email."</li>
        <li style='margin: 0 0 5px 0;'><b>Telefono:</b> ".$phone."</li>
        <li style='margin: 0 0 5px 0;'><b>Persone:</b> ".$people."</li>
        <li style='margin: 0 0 5px 0;'><b>Data di arrivo:</b> ".$calendarstart."</li>
        <li style='margin: 0 0 5px 0;'><b>Data di partenza:</b> ".$calendarend."</li>
        <li style='margin: 0 0 5px 0;'><b>Messaggio:</b> ".$message."</li>
        <li style='margin: 0 0 5px 0;'><b>Accettazione Privacy:</b> ".$checkbox."</li>
        </ul>
        ";
        // impostare sempre content-type quando si invia una mail in HTML
        $headers = "MIME-Version: 1.0" . "\r\n";
        $headers .= "Content-type:text/html;charset=UTF-8" . "\r\n";
        // headers
        $headers .= 'From:'.$name.' <'.$email.'>' . "\r\n";
        // invio mail
        @mail($to,$subject,$htmlContent,$headers);
        $succMsg = '<script>
        UIkit.notify({
          message : "Message sent!",
          status  : "success",
          timeout : 0,
          pos     : "top-center"
        });
        </script>';
        $name = '';
        $email = '';
        $phone = '';
        $people = '';
        $calendarstart = '';
        $calendarend = '';
        $message = '';
        $checkbox = '';
      else:
        $errMsg = '<script>
        UIkit.notify({
          message : "Message not sent!",
          status  : "danger",
          timeout : 0,
          pos     : "top-center"
        });
        </script>';
      endif;
    else:
      $errMsg = '<script>
      UIkit.notify({
        message : "Please click on the reCAPTCHA box.",
        status  : "warning",
        timeout : 0,
        pos     : "top-center"
      });
      </script>';
    endif;
  else:
    $errMsg = '';
    $succMsg = '';
    $name = '';
    $email = '';
    $phone = '';
    $people = '';
    $calendarstart = '';
    $calendarend = '';
    $message = '';
    $checkbox = '';
  endif;
  ?>
  <!-- risultato -->
  <?php if(!empty($errMsg)): ?><div class="errMsg"><?php echo $errMsg; ?></div><?php endif; ?>
  <?php if(!empty($succMsg)): ?><div class="succMsg"><?php echo $succMsg; ?></div><?php endif; ?>