Sindbad~EG File Manager

Current Path : /home/copmadinaarea/thecopmadinaarea.org/attendance/admin/
Upload File :
Current File : /home/copmadinaarea/thecopmadinaarea.org/attendance/admin/test_smtp.php

<?php
require_once '../config/config.php';
require_once '../includes/smtp_mailer.php';

// Check if user is logged in and has proper permissions
if (!isLoggedIn()) {
    redirect('login.php');
}

// Only superusers can access SMTP test
if (!hasRole('superuser')) {
    redirect('dashboard.php');
}

$db = new Database();
$conn = $db->getConnection();

$test_result = '';
$error_message = '';

if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['test_smtp'])) {
    if (!validateCSRFToken($_POST['csrf_token'] ?? '')) {
        $error_message = 'Invalid security token. Please try again.';
    } else {
        $test_email = sanitizeInput($_POST['test_email'] ?? '');
        
        if (empty($test_email) || !filter_var($test_email, FILTER_VALIDATE_EMAIL)) {
            $error_message = 'Please enter a valid email address for testing.';
        } else {
            // Get email settings
            $settings_query = "SELECT * FROM email_settings WHERE is_active = 1 ORDER BY created_at DESC LIMIT 1";
            $settings_stmt = $conn->prepare($settings_query);
            $settings_stmt->execute();
            $email_settings = $settings_stmt->fetch();
            
            if (!$email_settings) {
                $error_message = 'Email settings not configured. Please configure SMTP settings first.';
            } else {
                // Test SMTP connection
                $subject = 'SMTP Test Email - Church Attendance System';
                $body = "This is a test email to verify SMTP configuration.\n\n";
                $body .= "Test details:\n";
                $body .= "- SMTP Host: {$email_settings['smtp_host']}\n";
                $body .= "- SMTP Port: {$email_settings['smtp_port']}\n";
                $body .= "- Encryption: {$email_settings['smtp_encryption']}\n";
                $body .= "- From: {$email_settings['from_email']}\n";
                $body .= "- Test Time: " . date('Y-m-d H:i:s') . "\n\n";
                $body .= "If you received this email, your SMTP configuration is working correctly!";
                
                $result = sendEmailViaSMTP($email_settings, $test_email, $subject, $body);
                
                if ($result) {
                    $test_result = "✅ SMTP test successful! Email sent to $test_email";
                } else {
                    $error_message = "❌ SMTP test failed. Check the error details below and verify your SMTP settings.";
                }
            }
        }
    }
}

// Get current email settings for display
$settings_query = "SELECT * FROM email_settings ORDER BY created_at DESC LIMIT 1";
$settings_stmt = $conn->prepare($settings_query);
$settings_stmt->execute();
$email_settings = $settings_stmt->fetch();

// Get site settings
$query = "SELECT setting_key, setting_value FROM settings WHERE setting_key IN ('site_title', 'site_logo')";
$stmt = $conn->prepare($query);
$stmt->execute();
$site_settings = [];
while ($row = $stmt->fetch()) {
    $site_settings[$row['setting_key']] = $row['setting_value'];
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>SMTP Test - <?php echo $site_settings['site_title'] ?? SITE_TITLE; ?></title>
    <script src="https://cdn.tailwindcss.com"></script>
    <script>
        tailwind.config = {
            theme: {
                extend: {
                    colors: {
                        primary: '#3B82F6',
                        secondary: '#F59E0B',
                        accent: '#6B7280'
                    }
                }
            }
        }
    </script>
    <link href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.0.0/css/all.min.css" rel="stylesheet">
    <style>
        .gradient-bg {
            background: linear-gradient(135deg, #3B82F6 0%, #F59E0B 50%, #6B7280 100%);
        }
    </style>
</head>
<body class="bg-gray-50">
    <!-- Include Sidebar -->
    <?php include 'includes/sidebar.php'; ?>

    <!-- Main Content -->
    <div class="md:ml-64">
        <!-- Header -->
        <header class="bg-white shadow-sm border-b">
            <div class="px-6 py-4">
                <h1 class="text-2xl font-bold text-gray-900">SMTP Connection Test</h1>
            </div>
        </header>

        <!-- Content -->
        <main class="p-6">
            <div class="max-w-2xl mx-auto">
                <!-- Test Result Messages -->
                <?php if ($test_result): ?>
                    <div class="bg-green-50 border border-green-200 text-green-700 px-4 py-3 rounded-lg mb-6">
                        <i class="fas fa-check-circle mr-2"></i>
                        <?php echo $test_result; ?>
                    </div>
                <?php endif; ?>

                <?php if ($error_message): ?>
                    <div class="bg-red-50 border border-red-200 text-red-700 px-4 py-3 rounded-lg mb-6">
                        <i class="fas fa-exclamation-triangle mr-2"></i>
                        <?php echo $error_message; ?>
                    </div>
                <?php endif; ?>

                <!-- Current Settings Display -->
                <?php if ($email_settings): ?>
                <div class="bg-white rounded-lg shadow mb-8">
                    <div class="px-6 py-4 border-b border-gray-200">
                        <h2 class="text-xl font-semibold text-gray-900">
                            <i class="fas fa-cog mr-2 text-primary"></i>Current SMTP Settings
                        </h2>
                    </div>
                    <div class="p-6">
                        <div class="grid grid-cols-1 md:grid-cols-2 gap-4 text-sm">
                            <div>
                                <label class="font-medium text-gray-700">SMTP Host:</label>
                                <p class="text-gray-900"><?php echo htmlspecialchars($email_settings['smtp_host']); ?></p>
                            </div>
                            <div>
                                <label class="font-medium text-gray-700">SMTP Port:</label>
                                <p class="text-gray-900"><?php echo $email_settings['smtp_port']; ?></p>
                            </div>
                            <div>
                                <label class="font-medium text-gray-700">Username:</label>
                                <p class="text-gray-900"><?php echo htmlspecialchars($email_settings['smtp_username']); ?></p>
                            </div>
                            <div>
                                <label class="font-medium text-gray-700">Encryption:</label>
                                <p class="text-gray-900"><?php echo strtoupper($email_settings['smtp_encryption']); ?></p>
                            </div>
                            <div>
                                <label class="font-medium text-gray-700">From Email:</label>
                                <p class="text-gray-900"><?php echo htmlspecialchars($email_settings['from_email']); ?></p>
                            </div>
                            <div>
                                <label class="font-medium text-gray-700">From Name:</label>
                                <p class="text-gray-900"><?php echo htmlspecialchars($email_settings['from_name']); ?></p>
                            </div>
                        </div>
                    </div>
                </div>
                <?php else: ?>
                <div class="bg-yellow-50 border border-yellow-200 text-yellow-700 px-4 py-3 rounded-lg mb-6">
                    <i class="fas fa-exclamation-triangle mr-2"></i>
                    No email settings configured. Please configure SMTP settings first.
                </div>
                <?php endif; ?>

                <!-- Test Form -->
                <div class="bg-white rounded-lg shadow">
                    <div class="px-6 py-4 border-b border-gray-200">
                        <h2 class="text-xl font-semibold text-gray-900">
                            <i class="fas fa-paper-plane mr-2 text-primary"></i>Send Test Email
                        </h2>
                    </div>
                    <div class="p-6">
                        <form method="POST">
                            <input type="hidden" name="csrf_token" value="<?php echo generateCSRFToken(); ?>">
                            
                            <div class="mb-6">
                                <label for="test_email" class="block text-sm font-medium text-gray-700 mb-2">
                                    Test Email Address
                                </label>
                                <input type="email" 
                                       id="test_email" 
                                       name="test_email" 
                                       class="w-full px-3 py-2 border border-gray-300 rounded-lg focus:ring-2 focus:ring-primary focus:border-transparent"
                                       placeholder="test@example.com" 
                                       required>
                                <p class="text-sm text-gray-500 mt-2">
                                    Enter an email address where you can receive the test email
                                </p>
                            </div>

                            <button type="submit" 
                                    name="test_smtp" 
                                    class="w-full bg-primary text-white py-3 px-6 rounded-lg hover:bg-blue-700 transition duration-300 font-semibold"
                                    <?php echo !$email_settings ? 'disabled' : ''; ?>>
                                <i class="fas fa-paper-plane mr-2"></i>Send Test Email
                            </button>
                        </form>
                    </div>
                </div>

                <!-- Instructions -->
                <div class="mt-8 bg-blue-50 border border-blue-200 rounded-lg p-6">
                    <h3 class="text-lg font-semibold text-blue-900 mb-4">
                        <i class="fas fa-info-circle mr-2"></i>How to Use This Test
                    </h3>
                    <div class="text-sm text-blue-800 space-y-2">
                        <p>1. Make sure your SMTP settings are configured in <a href="email_management.php" class="underline">Email Management</a></p>
                        <p>2. Enter a valid email address where you can receive emails</p>
                        <p>3. Click "Send Test Email" to test the connection</p>
                        <p>4. Check your email inbox (and spam folder) for the test message</p>
                        <p>5. If the test fails, check <a href="email_diagnostics.php" class="underline">Email Diagnostics</a> for detailed error information</p>
                    </div>
                </div>

                <!-- Quick Links -->
                <div class="mt-6 text-center space-x-4">
                    <a href="email_management.php" class="bg-gray-600 text-white px-4 py-2 rounded-lg hover:bg-gray-700 transition duration-300 inline-block">
                        <i class="fas fa-cog mr-2"></i>Email Settings
                    </a>
                    <a href="email_diagnostics.php" class="bg-secondary text-white px-4 py-2 rounded-lg hover:bg-yellow-600 transition duration-300 inline-block">
                        <i class="fas fa-stethoscope mr-2"></i>Diagnostics
                    </a>
                    <a href="email_setup_guide.php" class="bg-blue-600 text-white px-4 py-2 rounded-lg hover:bg-blue-700 transition duration-300 inline-block">
                        <i class="fas fa-question-circle mr-2"></i>Setup Guide
                    </a>
                </div>
            </div>
        </main>
    </div>
</body>
</html>

Sindbad File Manager Version 1.0, Coded By Sindbad EG ~ The Terrorists