<?php

        header('Location: index.php#ContactUs');

        require 'vendor/phpmailer/phpmailer/PHPMailerAutoload.php';

        $errors = [];



        if(isset($_POST['name'], $_POST['email'], $_POST['message'], $_POST['phone'])){


            $fields = [
                'name' => $_POST['name'],
                'email' => $_POST['email'],
                'message' => $_POST['message'],
                'phone' => $_POST['phone']
            ];

            foreach($fields as $field => $data){
                if(empty($data)){

                    $errors[] = 'The '. $field .' field is required.';


                }


            }

            if(isset($_POST['submit'])) {

                $secretKey = '6LfB0kEUAAAAAFZseEToGc-iEx4ubueQDlTlqcOj';
                $responseKey = $_POST['g-recaptcha-response'];
                $userIP = $_SERVER['REMOTE_ADDR'];

                $url = "https://www.google.com/recaptcha/api/siteverify?secret=$secretKey&response=$responseKey&remoteip=$userIP";
                $response = file_get_contents($url);
                $response = json_decode($response);

                if ($response->success){

                }else {
                    $errors[] = "You didn't pass verification test.";
                }


            }



            if(empty($errors)) {

                $mail = new PHPMailer;

                //$mail->SMTPDebug = 3;                               // Enable verbose debug output

                $mail->isSMTP();                                      // Set mailer to use SMTP
                $mail->Host = 'smtp.gmail.com';  // Specify main and backup SMTP servers
                $mail->SMTPAuth = true;                               // Enable SMTP authentication
                $mail->Username = 'branislavcustomers@gmail.com';                 // SMTP username
                $mail->Password = 'e/b/By2W,fDrd[YV';                           // SMTP password
                $mail->SMTPSecure = 'ssl';                            // Enable TLS encryption, `ssl` also accepted
                $mail->Port = 465;                                    // TCP port to connect to

                $mail->setFrom('branislavcustomers@gmail.com', 'Customer From Website');
                $mail->addAddress('paintingkantor@gmail.com', 'Brigita');     // Add a recipient

                $mail->isHTML(true);                                  // Set email format to HTML

                $mail->Subject = 'Here is the subject';
                $mail->Body    =  'From: ' . $fields['name'] . '(' . $fields['email'] . ' Phone: ' . $fields['phone'] . ')<p>' . $fields['message'] . '</p>';
                $mail->AltBody = 'This is the body in plain text for non-HTML mail clients';

                if(!$mail->send()) {
                    echo 'Message could not be sent.';
                } else {
                    header("Refresh:0; url=contact");
                    $_SESSION['sentemail'] = "Thank you for your email!";

                }

            }





        }else{
            $errors[] = 'Something went wrong';
        }




        $_SESSION['errors'] = $errors;
        $_SESSION['fields'] = $fields;

        ?>

