<?php require_once 'config/connect.php';
require_once __DIR__ . '/vendor/autoload.php';

use PHPMailer\PHPMailer\Exception;
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\SMTP;

$mail = new PHPMailer(true);

// ini_set('display_errors', 1);
// ini_set('display_startup_errors', 1);
// error_reporting(E_ALL);
ini_set('max_execution_time', '0'); // for infinite time of execution

if (isset($_POST['regfsmt']) && $_POST['regfsmt'] == 'frmscnt') {
    $errormsg = "";
    $dateTimeNow = date('Y-m-d H:i:s');

    $data = array();
    $data['email'] = ($_POST['newsletter_email']) ? MRES_FILTER($_POST['newsletter_email']) : "";
    $data['created_at'] = $dateTimeNow;
    $subject = "NEWSLETTER DETAILS";

    $thankByName = "VELOCITY8 CLUB";
    if ($errormsg != "") {
        $_SESSION['msg_err'] = $errormsg;
        echo '<meta http-equiv="refresh" content="0;url=index.html" />';
        exit();
    }

    // print_r($data);
    // exit();
    $insertedid = jRInsert("newsletter", $data);
    if ($allownotinsert == 1) {
        $insertedid = 1;
    }
    if ($insertedid > 0) {
        try {

            $email_txt = "<html>
                <head>
                  <title></title>
                  <style type='text/css'>
                    body {
                      width: 100%;
                      margin: 0;
                      padding: 0;
                    }
                    table tbody tr td {
                      padding: 10px 5px;
                    }
                  </style>
                </head>



                <div style='border:1px solid'>
                <div style='background:#fff; padding:10px; text-align:center;'><img src='https://velocity8club.com/assets/img/logo.png' alt='" . $emailTitle . "'  title='" . $emailTitle . "'  /></div>
                <div style='background:#fff; padding:10px; text-align:center;'>" . $emailTitle2 . "</div>


                  <div style=' padding:10px;'>

                        <table>
                        <tbody>



                        <tr>
                            <td><strong>Email</strong></td>
                            <td>:</td>
                            <td>" . $data['email'] . "</td>
                        </tr>


                        <tr>
                            <td><strong>Date</strong></td>
                            <td>:</td>
                            <td>" . date('d-m-Y H:i:s', strtotime($data['created_at'])) . "</td>
                        </tr>
                    </tbody>
                    </table>
                    <p><strong>Thank You !</strong><br>" . $thankByName . "</p>

                  </div>
                </div>
                </html>";

            $contact_from_name = 'VELOCITY8 CLUB NEWSLETTER ';
            // Header for sender info

            try {
                //Server settings
                $mail->SMTPDebug = SMTP::DEBUG_OFF; //Enable verbose debug output DEBUG_OFF
                $mail->isSMTP(); //Send using SMTP
                $mail->Host = 'velocity8club.com'; //Set the SMTP server to send through   (mail.asaraa.com)
                $mail->SMTPAuth = true; //Enable SMTP authentication
                $mail->Username = 'noreply@velocity8club.com'; //SMTP username
                $mail->Password = '[AzuLMIqapl['; //SMTP password
                $mail->SMTPSecure = PHPMailer::ENCRYPTION_SMTPS; //Enable implicit TLS encryption ENCRYPTION_SMTPS
                $mail->Port = 465; //TCP port to connect to; use 465 587 if you have set `SMTPSecure = PHPMailer::ENCRYPTION_STARTTLS`

                $mail->setFrom($contact_from_email, $contact_from_name);
                // $mail->addAddress($data['email'], $data['name']);
                $mail->addAddress('info@velocity8club.com', 'NEWSLETTER');
                $mail->addAddress($data['email']);
                $mail->addReplyTo('info@velocity8club.com');
                $mail->addBCC('akkiniraj@gmail.com');

                // if ($filefullpath != "") {
                //     //Attachments
                //     $mail->addAttachment($filefullpath); //Optional name
                // }

                //Content
                $mail->isHTML(true); //Set email format to HTML
                $mail->Subject = $subject;
                $mail->Body = $email_txt;
                $mail->AltBody = '';

                $mail->send();

                // print_r($data);
                // exit();
                $_SESSION['msg_succ'] = "Thank you for signing up. You will now receive exciting updates to your email! ";
                echo '<meta http-equiv="refresh" content="0;index.html" />';
                exit();
            } catch (Exception $e) {
                // echo "Message could not be sent. Mailer Error: {$mail->ErrorInfo}";
                // exit();
                // print_r($data);
                // exit();
                $_SESSION['msg_succ'] = "Thank you for signing up. You will now receive exciting updates to your email! ";
                echo '<meta http-equiv="refresh" content="0;url=index.html" />';
                exit();
            }
        } catch (Exception $e) {
            // echo $e->getMessage();
            $_SESSION['msg_err'] = "Please Try again after some time.";
            echo '<meta http-equiv="refresh" content="0;url=index.html" />';
            exit();
        } catch (InvalidArgumentException $e) {
            // echo $e->getMessage();
            $_SESSION['msg_err'] = "Please Try again after some time.";
            echo '<meta http-equiv="refresh" content="0;url=index.html" />';
            exit();
        }
    } else {
        $_SESSION['msg_err'] = "Please Try again after some time.";

        echo '<meta http-equiv="refresh" content="0;url=index.html" />';
        exit();
    }
}
