<?php
/**
 * Makhanani Digital Solutions - Contact Form Handler
 * 
 * This handles the contact form submissions.
 * Update the $receiving_email_address with your actual email.
 */

// Replace with your real email address
$receiving_email_address = 'info@makhananidigital.co.za';

// Check if PHP Email Form library exists
if (file_exists($php_email_form = '../assets/vendor/php-email-form/php-email-form.php')) {
    include($php_email_form);
} else {
    // Fallback: Use native PHP mail function
    
    // Validate required fields
    if (empty($_POST['name']) || empty($_POST['email']) || empty($_POST['message'])) {
        http_response_code(400);
        echo 'Please fill in all required fields.';
        exit;
    }

    // Sanitize inputs
    $name = htmlspecialchars(strip_tags(trim($_POST['name'])));
    $email = filter_var(trim($_POST['email']), FILTER_SANITIZE_EMAIL);
    $company = isset($_POST['company']) ? htmlspecialchars(strip_tags(trim($_POST['company']))) : 'Not provided';
    $phone = isset($_POST['phone']) ? htmlspecialchars(strip_tags(trim($_POST['phone']))) : 'Not provided';
    $message = htmlspecialchars(strip_tags(trim($_POST['message'])));

    // Validate email
    if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
        http_response_code(400);
        echo 'Please enter a valid email address.';
        exit;
    }

    // Build email content
    $subject = "New Contact Form Submission from $name";
    
    $email_content = "You have received a new message from your website contact form.\n\n";
    $email_content .= "Name: $name\n";
    $email_content .= "Email: $email\n";
    $email_content .= "Company: $company\n";
    $email_content .= "Phone: $phone\n\n";
    $email_content .= "Message:\n$message\n";

    // Email headers
    $headers = "From: $name <$email>\r\n";
    $headers .= "Reply-To: $email\r\n";
    $headers .= "X-Mailer: PHP/" . phpversion();

    // Send email
    if (mail($receiving_email_address, $subject, $email_content, $headers)) {
        http_response_code(200);
        echo 'Your message has been sent. Thank you!';
    } else {
        http_response_code(500);
        echo 'Oops! Something went wrong. Please try again later.';
    }
    
    exit;
}

// If PHP Email Form library exists, use it
$contact = new PHP_Email_Form;
$contact->ajax = true;

$contact->to = $receiving_email_address;
$contact->from_name = $_POST['name'];
$contact->from_email = $_POST['email'];
$contact->subject = "New Contact Form Submission from " . $_POST['name'];

// Uncomment and configure SMTP if needed
/*
$contact->smtp = array(
    'host' => 'smtp.example.com',
    'username' => 'your-email@example.com',
    'password' => 'your-password',
    'port' => '587'
);
*/

$contact->add_message($_POST['name'], 'From');
$contact->add_message($_POST['email'], 'Email');
isset($_POST['company']) && $contact->add_message($_POST['company'], 'Company');
isset($_POST['phone']) && $contact->add_message($_POST['phone'], 'Phone');
$contact->add_message($_POST['message'], 'Message', 10);

echo $contact->send();
?>
