· 7 years ago · Jan 07, 2019, 05:22 PM
1<?php
2 $msg = "";
3 use PHPMailer\PHPMailer\PHPMailer;
4 use PHPMailer\PHPMailer\Exception;
5
6 include_once "phpmailer/PHPMailer.php";
7 include_once "phpmailer/Exception.php";
8 include_once "phpmailer/SMTP.php";
9
10 if (isset($_POST['submit'])) {
11 $subject = $_POST['subject'];
12 $email = $_POST['email'];
13 $message = $_POST['message'];
14 $name = $_POST['name'];
15
16 $recaptcha = false;
17 $secretKey = 'xxxxxxxxxxxxxxxxxxxxxxxxxxx';
18 $responseKey = $_POST['g-recaptcha-response'];
19 $userIP = $_SERVER['REMOTE_ADDR'];
20 $url = "https://www.google.com/recaptcha/api/siteverify?secret=$secretKey&response=$responseKey&remoteip=$userIP";
21 $response = file_get_contents($url);
22 $response = json_decode($response);
23
24 if ($response->success == true) {
25 $recaptcha = true;
26 echo 'Verification success.';
27 }else{
28 echo 'Verification failed.';
29 }
30
31
32 if($recaptcha){
33 $mail = new PHPMailer(true);
34 //$mail->SMTPDebug = 2;
35 $mail->addAddress('xxxxxxxxxxxx@tiscali.it');
36 $mail->setFrom($email);
37 $mail->Subject = $subject;
38 $mail->isHTML(true);
39 $mail->Body = $message;
40 if ($mail->send()){
41 $msg = "<p style='color: green; font-style: italic; font-weight: bold;'>Email inviata con successo!</p>";
42 }else{
43 $msg = "<p style='color: red; font-style: italic; font-weight: bold;'>Si è verificato un errore. Riprova più tardi.</p>";
44 }
45 }else{
46 $msg = "<p style='color: red; font-style: italic; font-weight: bold;'>Si è verificato un errore. CAPTCHA non validato.</p>";
47 }
48 }
49?>
50
51<!DOCTYPE html>
52<html lang="it">
53<head>
54<meta charset="utf-8">
55<title></title>
56</head>
57<body>
58<form>
59<div class="g-recaptcha" data-sitekey="xxxxxxxxxxxxxxxxxxxxxxxxxx"></div>
60</form>
61
62<script src='https://www.google.com/recaptcha/api.js'></script>
63</body>
64</html>