<?php

require 'vendor/autoload.php';

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

// Define database connection parameters
$host = "localhost";
$username = "#";
$password = "#";
$database = "#";

// Attempt to establish a connection to the database
$db = new mysqli($host, $username, $password, $database);

// Check if the connection was successful
if ($db->connect_error) {
    die("Connection failed: " . $db->connect_error);
}

// Check if form is submitted
if ($_SERVER["REQUEST_METHOD"] == "POST") {
    // Collect form data
    $name = $_POST['name'];
    $email = $_POST['email'];
    $phone = $_POST['phone'];
    $subject = $_POST['subject'];
    $message = $_POST['msg'];

    // Insert form data into the database
    $stmt = $db->prepare("INSERT INTO `data`(`name`, `email`, `phone`, `subject`, `msg`) VALUES (?, ?, ?, ?, ?)");
    $stmt->bind_param("sssss", $name, $email, $phone, $subject, $message);
    if ($stmt->execute()) {
        // Prepare to send email to admin
        $mail = new PHPMailer(true);

        try {
            // Server settings
            $mail->isSMTP();                                 // Set mailer to use SMTP
            $mail->Host = 'smtp.gmail.com';                  // Specify main SMTP server
            $mail->SMTPAuth = true;                          // Enable SMTP authentication
            $mail->Username = 'email.com';       // SMTP username
            $mail->Password = 'pnhkdqvconnanpri';          // SMTP password
            $mail->SMTPSecure = PHPMailer::ENCRYPTION_STARTTLS; // Enable TLS encryption, `ssl` also accepted
            $mail->Port = 587;                               // TCP port to connect to

            // Recipients
            $mail->setFrom('email.com', 'name');
            $mail->addAddress('email.com', 'name'); // Add a recipient

            // Content
            $mail->isHTML(true);                             // Set email format to HTML
            $mail->Subject = $subject;
            $mail->Body    = "<p><strong>Name:</strong> $name</p>
                              <p><strong>Email:</strong> $email</p>
                              <p><strong>Phone:</strong> $phone</p>
                              <p><strong>Subject:</strong> $subject</p>
                              <p><strong>Message:</strong> $message</p>";
            $mail->AltBody = "Name: $name\nEmail: $email\nPhone: $phone\nSubject: $subject\nMessage: $message";

            // Send the email
            $mail->send();
            echo "<h2>Thanks! Your request has been submitted </h2>";

            // Prepare to send auto-reply email to the user
            $autoReply = new PHPMailer(true);
            $autoReply->isSMTP();
            $autoReply->Host = 'smtp.gmail.com';
            $autoReply->SMTPAuth = true;
            $autoReply->Username = 'email.com';
            $autoReply->Password = 'pnhkdqvconnanpri';
            $autoReply->SMTPSecure = PHPMailer::ENCRYPTION_STARTTLS;
            $autoReply->Port = 587;

            // Recipients
            $autoReply->setFrom('email.com', 'name');
            $autoReply->addAddress($email, $name); // Send to user's email address

            // Content
            $autoReply->isHTML(true);
            $autoReply->Subject = 'Thank you for your submission';
            $autoReply->Body    = "<p>Dear $name,<br/><br/>
                                   Thank you for reaching out to us. We have received your request and will get back to you shortly.<br/><br/>
                                   Thanks & Regards,<br/></br>

                                      
                                    
                                  name Team</p>";
            $autoReply->AltBody = "Dear $name,\n\nThank you for reaching out to us. We have received your request and will get back to you shortly.\n\nYour Submission Details:\nName: $name\nEmail: $email\nPhone: $phone\nSubject: $subject\nMessage: $message\n\nThanks,\n  Team";

            // Send the auto-reply email
            $autoReply->send();

            // Redirect after 3 seconds
              
            echo '<meta http-equiv="refresh" content="2;URL=\'index.php\'">';
            exit();
        } catch (Exception $e) {
            echo "Message could not be sent. Mailer Error: {$mail->ErrorInfo}";
        }
    } else {
        echo "Error: " . $stmt->error;
    }
}
