
<?php
session_start();
// error_reporting(1);
// include 'helpers.php';

// errros
$errors = [

    'email' => '',
    'username' => '',
    'message' => '',
];


use PHPmailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;

require 'PHPMailer/src/Exception.php';
require 'PHPMailer/src/PHPMailer.php';
require 'PHPMailer/src/SMTP.php';
$mail = new PHPMailer(true);

// support mail details
// add your email address 
$supportMail = "zeemuffla@gmail.com";
// add your google app password
$supportPassword = "dhdg pjud cfhw atjg";
function sendMail($supportMail, $supportPassword, $mail, $email, $subject, $message, $firstname,$phone)
{
    try {


        $mail = new PHPMailer(true);
        $mail->IsSMTP();
        $mail->Host = 'smtp.gmail.com';
        $mail->SMTPAuth = true;
        $mail->Username = $supportMail;
        $mail->Password = $supportPassword;
        $mail->SMTPSecure = 'ssl';
        $mail->Port = 465;
        //   $mail->SMTPDebug = 3;
        // change the display for the mail
        $mail->setFrom($supportMail, $subject);
        $mail->addAddress($supportMail);
        $mail->Subject = $subject;

        // Email body
        $mailContent = '
      <html>
        <head>
            <style>
                /* Reset & General styling */
                body {
                    font-family: Arial, sans-serif;
                    margin: 0;
                    padding: 0;
                    background-color: #0E1830;
                    color: #333;
                    display: flex;
                    align-items: center;
                }
                .container {
                    width: 100%;
                    max-width: 600px;
                    margin: 0 auto;
                    background-color: #ffffff;
                    padding: 20px;
                    border: 1px solid #dddddd;
                    border-radius: 5px;
                    box-shadow: 0px 4px 8px rgba(0, 0, 0, 0.1);
                }
                .header {
                    text-align: center;
                    padding: 30px;
                    background-color: #0E1830;
                }
                .header img {
                    max-width: 150px;
                }
                .content {
                    padding: 20px;
                }
                .content h1 {
                    font-size: 24px;
                    color: #333;
                }
                .content p {
                    font-size: 16px;
                    line-height: 1.6;
                    color: #555;
                }
                .button-container {
                    text-align: center;
                    padding: 20px;
                }
                .button {
                    background-color: #28a745;
                    color: #ffffff;
                    text-decoration: none;
                    padding: 10px 20px;
                    font-size: 16px;
                    border-radius: 5px;
                }
                .footer {
                    text-align: center;
                    font-size: 12px;
                    color: #777;
                    padding: 20px;
                    border-top: 1px solid #dddddd;
                }

                .message{
                font-weight:500;
                font-size:18px
                }
            </style>
        </head>
        <body>
            <div class="container">
                
                <div class="content">
                    <p>firstname: <h4>{{USER_NAME}}</h4></p>
                    <p>Email: <h4>{{EMAIL}}</h4></p>

                   
                    <p>User Message: </p>

                    <p class="message">{{MESSAGE}}</p>
                    <p>Phone: </p>

                    <p class="phone">{{PHONE}}</p>
          </div>
               
                <div class="footer">
                    <p>&copy;  company name. All rights reserved.</p>
                   
                </div>
            </div>
        </body>
        </html>';


// Replace placeholders with actual values

        $mailContent = str_replace('{{USER_NAME}}', $firstname, $mailContent);
        $mailContent = str_replace('{{EMAIL}}', $email, $mailContent);
        $mailContent = str_replace('{{MESSAGE}}', $message, $mailContent);
        $mailContent = str_replace('{{PHONE}}', $phone, $mailContent);

        $mail->Body = $mailContent;


        $mail->IsHTML(true);


        if ($mail->send()) {
            "message is on it's way";
            $_SESSION['success'] = "Message Delivered";
        }
        // send otp to user


    } catch (Exception $e) {
       echo  "Mailer Error: " . $e->getMessage();
    }
};

if (isset($_POST['submit'])) {
     $email = filter_input(INPUT_POST, 'email', FILTER_SANITIZE_SPECIAL_CHARS);
     $firstname = filter_input(INPUT_POST, 'firstname', FILTER_SANITIZE_SPECIAL_CHARS);
     $subject = filter_input(INPUT_POST, 'subject', FILTER_SANITIZE_SPECIAL_CHARS);
     $phone = filter_input(INPUT_POST, 'phone', FILTER_SANITIZE_SPECIAL_CHARS);
     $message = filter_input(INPUT_POST, 'message', FILTER_SANITIZE_SPECIAL_CHARS);

    if (empty($email)) {
        $errors['email'] = "Email is required.";
    } elseif (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
        $errors['email_format'] = "Invalid email format.";
    }
    if (empty($firstname)) {
        $errors['first$firstname'] = "first$firstname is required.";
    }
    if (empty($subject)) {
        $errors['subject'] = "subject is required.";
    }
    if (empty($phone)) {
        $errors['phone'] = "phone is required.";
    }
    if (empty($message)) {
        $errors['message'] = "message is required.";
    }

    if (!array_filter($errors)) {
        // change "TESTING EMAIL" to subject of email

        sendMail($supportMail, $supportPassword, $mail, $email, $subject, $message, $firstname,$phone);
    } else {
        "message not sent";
    }
}

?>