<?php
require_once 'includes/config.php';
require_once 'includes/email_config.php';
require_once 'includes/email_functions.php';

echo "<h2>Final Email Test with PHPMailer</h2>";

// Test email sending with PHPMailer
$to = SMTP_USERNAME;
$subject = 'StoreWeb Final Email Test - ' . date('Y-m-d H:i:s');
$message = '
<html>
<body>
    <h2>StoreWeb Email Test Successful!</h2>
    <p>This email was sent using PHPMailer with SMTP authentication.</p>
    <p><strong>Site:</strong> ' . SITE_NAME . '</p>
    <p><strong>Time:</strong> ' . date('Y-m-d H:i:s') . '</p>
    <p><strong>Server:</strong> ' . $_SERVER['SERVER_NAME'] . '</p>
    <p style="color: green; font-weight: bold;">Email configuration is working correctly!</p>
</body>
</html>
';

echo "<p>Sending test email to: <strong>$to</strong></p>";
echo "<p>Subject: <strong>$subject</strong></p>";

// Log the attempt
error_log("Final email test: Attempting to send test email to $to");

// Try to send email
if (sendEmail($to, $subject, $message)) {
    echo "<p style='color: green; font-weight: bold;'>✓ Email sent successfully!</p>";
    echo "<p>Please check your inbox (and spam folder) for the test email.</p>";
    echo "<h3>Next Steps:</h3>";
    echo "<ul>";
    echo "<li>All email notifications in your StoreWeb application should now work correctly</li>";
    echo "<li>Welcome emails will be sent when new users register</li>";
    echo "<li>Support ticket notifications will be sent to customers and agents</li>";
    echo "<li>Order confirmations and other notifications will work properly</li>";
    echo "</ul>";
} else {
    echo "<p style='color: red; font-weight: bold;'>✗ Failed to send email.</p>";
    echo "<h3>Troubleshooting:</h3>";
    echo "<ol>";
    echo "<li>Check your Gmail account security settings</li>";
    echo "<li>Ensure you're using an App Password if 2-Factor Authentication is enabled</li>";
    echo "<li>Verify SMTP settings in <code>includes/email_config.php</code></li>";
    echo "<li>Check PHP error logs for more details</li>";
    echo "</ol>";
}

echo "<p><a href='index.php'>Back to Home</a></p>";
?>