Sindbad~EG File Manager

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

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

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

// Only admin and superusers can access email diagnostics
if (!hasRole('admin') && !hasRole('superuser')) {
    redirect('dashboard.php');
}

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

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

// Handle test email
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['test_email_send'])) {
    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 {
            $result = testEmailConfiguration($conn, $test_email);
            if ($result) {
                $success_message = "Test email sent successfully to $test_email";
            } else {
                $error_message = "Test email failed to send to $test_email. Check the diagnostics below for details.";
            }
        }
    }
}

// Get email settings
$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 email templates
$templates_query = "SELECT * FROM email_templates WHERE template_name = 'attendance_confirmation'";
$templates_stmt = $conn->prepare($templates_query);
$templates_stmt->execute();
$email_template = $templates_stmt->fetch();

// Get recent email logs
$logs_query = "SELECT * FROM email_logs ORDER BY sent_at DESC LIMIT 20";
$logs_stmt = $conn->prepare($logs_query);
$logs_stmt->execute();
$email_logs = $logs_stmt->fetchAll();

// Get detailed email statistics
$email_stats = getEmailStats($conn);

// Check server configuration
$server_checks = [
    'mail_function' => function_exists('mail'),
    'smtp_configured' => !empty(ini_get('SMTP')) || !empty(ini_get('sendmail_path')),
    'php_version' => version_compare(PHP_VERSION, '7.0.0', '>='),
    'openssl' => extension_loaded('openssl'),
    'mbstring' => extension_loaded('mbstring')
];

// 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>Email Diagnostics - <?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">Email System Diagnostics</h1>
            </div>
        </header>

        <!-- Content -->
        <main class="p-6">
            <!-- Success/Error Messages -->
            <?php if ($success_message): ?>
                <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 $success_message; ?>
                </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; ?>

            <!-- Quick Test -->
            <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-paper-plane mr-2 text-primary"></i>Quick Email Test
                    </h2>
                </div>
                <div class="p-6">
                    <form method="POST" class="max-w-md">
                        <input type="hidden" name="csrf_token" value="<?php echo generateCSRFToken(); ?>">
                        <div class="flex">
                            <input type="email" name="test_email" 
                                   class="flex-1 px-3 py-2 border border-gray-300 rounded-l-lg focus:ring-2 focus:ring-primary focus:border-transparent"
                                   placeholder="test@example.com" required>
                            <button type="submit" name="test_email_send" 
                                    class="bg-primary text-white px-4 py-2 rounded-r-lg hover:bg-blue-700 transition duration-300">
                                <i class="fas fa-paper-plane mr-2"></i>Send Test
                            </button>
                        </div>
                    </form>
                </div>
            </div>

            <!-- System Status -->
            <div class="grid grid-cols-1 lg:grid-cols-2 gap-8 mb-8">
                <!-- Server Configuration -->
                <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-server mr-2 text-primary"></i>Server Configuration
                        </h2>
                    </div>
                    <div class="p-6">
                        <div class="space-y-4">
                            <?php foreach ($server_checks as $check => $status): ?>
                                <div class="flex items-center justify-between">
                                    <span class="text-sm font-medium text-gray-700">
                                        <?php echo ucwords(str_replace('_', ' ', $check)); ?>
                                    </span>
                                    <span class="flex items-center">
                                        <?php if ($status): ?>
                                            <i class="fas fa-check-circle text-green-600 mr-2"></i>
                                            <span class="text-green-600 text-sm">OK</span>
                                        <?php else: ?>
                                            <i class="fas fa-times-circle text-red-600 mr-2"></i>
                                            <span class="text-red-600 text-sm">Failed</span>
                                        <?php endif; ?>
                                    </span>
                                </div>
                            <?php endforeach; ?>
                        </div>
                    </div>
                </div>

                <!-- Email Settings Status -->
                <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-cog mr-2 text-primary"></i>Email Settings Status
                        </h2>
                    </div>
                    <div class="p-6">
                        <div class="space-y-4">
                            <div class="flex items-center justify-between">
                                <span class="text-sm font-medium text-gray-700">Settings Configured</span>
                                <span class="flex items-center">
                                    <?php if ($email_settings): ?>
                                        <i class="fas fa-check-circle text-green-600 mr-2"></i>
                                        <span class="text-green-600 text-sm">Yes</span>
                                    <?php else: ?>
                                        <i class="fas fa-times-circle text-red-600 mr-2"></i>
                                        <span class="text-red-600 text-sm">No</span>
                                    <?php endif; ?>
                                </span>
                            </div>
                            
                            <div class="flex items-center justify-between">
                                <span class="text-sm font-medium text-gray-700">Email Template Active</span>
                                <span class="flex items-center">
                                    <?php if ($email_template && $email_template['is_active']): ?>
                                        <i class="fas fa-check-circle text-green-600 mr-2"></i>
                                        <span class="text-green-600 text-sm">Yes</span>
                                    <?php else: ?>
                                        <i class="fas fa-times-circle text-red-600 mr-2"></i>
                                        <span class="text-red-600 text-sm">No</span>
                                    <?php endif; ?>
                                </span>
                            </div>
                            
                            <div class="flex items-center justify-between">
                                <span class="text-sm font-medium text-gray-700">Notifications Enabled</span>
                                <span class="flex items-center">
                                    <?php if ($email_settings && $email_settings['is_active']): ?>
                                        <i class="fas fa-check-circle text-green-600 mr-2"></i>
                                        <span class="text-green-600 text-sm">Yes</span>
                                    <?php else: ?>
                                        <i class="fas fa-times-circle text-red-600 mr-2"></i>
                                        <span class="text-red-600 text-sm">No</span>
                                    <?php endif; ?>
                                </span>
                            </div>
                        </div>
                    </div>
                </div>
            </div>

            <!-- Recent Failures -->
            <?php if (!empty($email_stats['recent_failures'])): ?>
            <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-exclamation-triangle mr-2 text-red-600"></i>Recent Email Failures
                    </h2>
                </div>
                <div class="overflow-x-auto">
                    <table class="min-w-full divide-y divide-gray-200">
                        <thead class="bg-gray-50">
                            <tr>
                                <th class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">Email</th>
                                <th class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">Subject</th>
                                <th class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">Error</th>
                                <th class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">Date</th>
                            </tr>
                        </thead>
                        <tbody class="bg-white divide-y divide-gray-200">
                            <?php foreach ($email_stats['recent_failures'] as $failure): ?>
                                <tr>
                                    <td class="px-6 py-4 whitespace-nowrap text-sm text-gray-900">
                                        <?php echo htmlspecialchars($failure['recipient_email']); ?>
                                    </td>
                                    <td class="px-6 py-4 whitespace-nowrap text-sm text-gray-900">
                                        <?php echo htmlspecialchars($failure['subject']); ?>
                                    </td>
                                    <td class="px-6 py-4 text-sm text-red-600">
                                        <?php echo htmlspecialchars($failure['error_message'] ?? 'Unknown error'); ?>
                                    </td>
                                    <td class="px-6 py-4 whitespace-nowrap text-sm text-gray-500">
                                        <?php echo date('M j, Y g:i A', strtotime($failure['sent_at'])); ?>
                                    </td>
                                </tr>
                            <?php endforeach; ?>
                        </tbody>
                    </table>
                </div>
            </div>
            <?php endif; ?>

            <!-- Email Logs -->
            <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-list mr-2 text-primary"></i>Recent Email Activity
                    </h2>
                </div>
                <div class="overflow-x-auto">
                    <table class="min-w-full divide-y divide-gray-200">
                        <thead class="bg-gray-50">
                            <tr>
                                <th class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">Status</th>
                                <th class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">Recipient</th>
                                <th class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">Subject</th>
                                <th class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">Date</th>
                            </tr>
                        </thead>
                        <tbody class="bg-white divide-y divide-gray-200">
                            <?php foreach ($email_logs as $log): ?>
                                <tr>
                                    <td class="px-6 py-4 whitespace-nowrap">
                                        <?php if ($log['status'] === 'sent'): ?>
                                            <span class="inline-flex items-center px-2.5 py-0.5 rounded-full text-xs font-medium bg-green-100 text-green-800">
                                                <i class="fas fa-check mr-1"></i>Sent
                                            </span>
                                        <?php else: ?>
                                            <span class="inline-flex items-center px-2.5 py-0.5 rounded-full text-xs font-medium bg-red-100 text-red-800">
                                                <i class="fas fa-times mr-1"></i>Failed
                                            </span>
                                        <?php endif; ?>
                                    </td>
                                    <td class="px-6 py-4 whitespace-nowrap text-sm text-gray-900">
                                        <?php echo htmlspecialchars($log['recipient_email']); ?>
                                    </td>
                                    <td class="px-6 py-4 text-sm text-gray-900">
                                        <?php echo htmlspecialchars($log['subject']); ?>
                                    </td>
                                    <td class="px-6 py-4 whitespace-nowrap text-sm text-gray-500">
                                        <?php echo date('M j, Y g:i A', strtotime($log['sent_at'])); ?>
                                    </td>
                                </tr>
                            <?php endforeach; ?>
                        </tbody>
                    </table>
                </div>
            </div>

            <!-- Troubleshooting Tips -->
            <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-lightbulb mr-2"></i>Troubleshooting Tips
                </h3>
                <div class="text-sm text-blue-800 space-y-2">
                    <p><strong>Common Issues:</strong></p>
                    <ul class="list-disc list-inside space-y-1 ml-4">
                        <li>XAMPP/Local development: PHP mail() function often doesn't work without SMTP configuration</li>
                        <li>Gmail SMTP: Use App Passwords instead of regular passwords</li>
                        <li>Firewall: Check if port 587 (TLS) or 465 (SSL) is blocked</li>
                        <li>Email settings: Verify SMTP host, port, username, and password are correct</li>
                        <li>Template issues: Ensure email template is active and properly formatted</li>
                    </ul>
                    <p class="mt-4"><strong>Recommended Solutions:</strong></p>
                    <ul class="list-disc list-inside space-y-1 ml-4">
                        <li>Install PHPMailer for better SMTP support</li>
                        <li>Use a reliable SMTP service (Gmail, SendGrid, Mailgun)</li>
                        <li>Test with a simple email service first</li>
                        <li>Check server error logs for detailed error messages</li>
                    </ul>
                </div>
            </div>
        </main>
    </div>
</body>
</html>

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