<?php
echo "<h1>Server Mail Sending Status Check</h1>";
echo "<pre>";

// ================== 1. Which MTA is running? ==================
$output = [];
exec("ps aux | grep -E 'postfix|exim|sendmail' | grep -v grep", $output);
echo "<strong>Running MTA:</strong>\n" . implode("\n", $output ?: ["No MTA detected"]) . "\n\n";

// ================== 2. Mail Queue Check ==================
$queue_count = 0;

if (function_exists('shell_exec')) {
    
    // Postfix Queue
    $postfix_queue = shell_exec("postqueue -p 2>/dev/null");
    if ($postfix_queue) {
        echo "<strong>Postfix Queue:</strong>\n" . $postfix_queue . "\n";
        $queue_count = (int)shell_exec("postqueue -p 2>/dev/null | tail -n 1 | awk '{print \$5}' 2>/dev/null");
    }

    // Exim Queue
    $exim_queue = shell_exec("exim -bpc 2>/dev/null");
    if ($exim_queue) {
        echo "<strong>Exim Queue Count:</strong> " . trim($exim_queue) . " emails\n";
        $queue_count = (int)trim($exim_queue);
    }

    // General mailq
    $mailq = shell_exec("mailq 2>/dev/null | tail -n 1");
    if ($mailq && strpos($mailq, 'Total') !== false) {
        echo "<strong>Mail Queue (mailq):</strong>\n" . $mailq . "\n";
    }
} else {
    echo "Warning: shell_exec() function is disabled on this server.\n";
}

echo "\n<strong>Current emails in Queue ≈ " . $queue_count . "</strong>\n\n";

// ================== 3. Recent Log Check (for limit/rate issues) ==================
$log_paths = [
    '/var/log/maillog',
    '/var/log/mail.log',
    '/var/log/exim_mainlog',
    '/var/log/exim/mainlog'
];

foreach ($log_paths as $log) {
    if (file_exists($log)) {
        $recent = shell_exec("tail -n 30 " . escapeshellarg($log) . " 2>/dev/null");
        if ($recent) {
            echo "<strong>Recent Logs from " . $log . ":</strong>\n" . htmlspecialchars($recent) . "\n\n";
            
            // Check for limit related keywords
            if (stripos($recent, 'limit') !== false || 
                stripos($recent, 'rate') !== false || 
                stripos($recent, 'too many') !== false || 
                stripos($recent, '421') !== false || 
                stripos($recent, 'deferred') !== false) {
                echo "⚠️ Warning: Log shows possible rate limit or delivery issues!\n";
            }
        }
    }
}

// ================== 4. cPanel Detection ==================
if (file_exists('/usr/local/cpanel/version')) {
    echo "<strong>cPanel Detected.</strong> Usually hosting providers set Hourly Email Limit (e.g. 200-2000 emails per hour).\n";
    echo "Check inside cPanel: Metrics → Email Reports or WHM → View Sent Summary\n\n";
}

// ================== 5. Summary ==================
echo "<h2>Summary:</h2>";

if ($queue_count > 50) {
    echo "❌ Queue is heavily loaded. You may have hit the sending limit or there is a delivery problem.\n";
} elseif ($queue_count > 10) {
    echo "⚠️ Some emails are stuck in the queue.\n";
} else {
    echo "✅ Queue is empty or very low. Sending status looks normal.\n";
}

echo "\n<strong>Best ways to know your actual limit:</strong>\n";
echo "1. Check cPanel → Metrics → Email Reports\n";
echo "2. Look for words like 'rate limit', 'too many', '421' in the logs\n";
echo "3. Contact your hosting support and ask: What is my Hourly/Daily email sending limit?\n";

echo "</pre>";
?>