· 7 years ago · Jan 07, 2019, 10:22 AM
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 = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx';
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) {
25 $recaptcha = true;
26
27 $msg = "<p style='color: green; font-style: italic; font-weight: bold;'>Ottimo, non sei un Robot!</p>";
28 }
29 else {
30 $msg = "<p style='color: red; font-style: italic; font-weight: bold;'>Dimostraci di essere un umano, clicca su 'Non sono un Robot'!</p>";
31 }
32
33 $mail = new PHPMailer();
34
35 if ($recaptcha) {
36 $mail->addAddress('davidemarca@tiscali.it');
37 $mail->setFrom($email);
38 $mail->Subject = $subject;
39 $mail->isHTML(true);
40 $mail->Body = $message;
41
42 if ($mail->send()) {
43 $msg = "<p style='color: green; font-style: italic; font-weight: bold;'>Email inviata con successo!</p>";
44 }
45 else {
46 $msg = "<p style='color: red; font-style: italic; font-weight: bold;'>Si è verificato un errore. Riprova più tardi.</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>