How can PHP be used to sort and display emails from a catch-all mailbox in a database based on the recipient's email address for user access?

To sort and display emails from a catch-all mailbox in a database based on the recipient's email address for user access, you can use PHP to query the database for emails matching the recipient's address and then display them in a user-friendly format.

// Connect to the database
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "emails";

$conn = new mysqli($servername, $username, $password, $dbname);

// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
}

// Query the database for emails based on recipient's email address
$recipient_email = "user@example.com";
$sql = "SELECT * FROM emails WHERE recipient_email = '$recipient_email'";
$result = $conn->query($sql);

// Display the emails in a user-friendly format
if ($result->num_rows > 0) {
    while($row = $result->fetch_assoc()) {
        echo "Subject: " . $row["subject"]. " - From: " . $row["sender_email"]. " - Date: " . $row["date"]. "<br>";
        echo "Message: " . $row["message"]. "<br><br>";
    }
} else {
    echo "No emails found for recipient: $recipient_email";
}

// Close the database connection
$conn->close();