· 7 years ago · Jan 07, 2019, 04:08 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 = '6LeL4IQUAAAAALHlV2EIQNBkPN9qrKcyAl3Ja1u5';
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 $response = print_r($response->success);
24
25 if ($response->success == false) {
26 $recaptcha = true;
27 echo 'Verification failed.';
28 }else{
29 echo 'Verification success.';
30 }
31
32
33 if($recaptcha){
34 $mail = new PHPMailer(true);
35 $mail->SMTPDebug = 2;
36 $mail->addAddress('davidemarca@tiscali.it');
37 $mail->setFrom($email);
38 $mail->Subject = $subject;
39 $mail->isHTML(true);
40 $mail->Body = $message;
41 if ($mail->send()){
42 $msg = "<p style='color: green; font-style: italic; font-weight: bold;'>Email inviata con successo!</p>";
43 }else{
44 $msg = "<p style='color: red; font-style: italic; font-weight: bold;'>Si è verificato un errore. Riprova più tardi.</p>";
45 }
46 }else{
47 $msg = "<p style='color: red; font-style: italic; font-weight: bold;'>Si è verificato un errore. CAPTCHA non validato.</p>";
48 }
49 }
50?>