How can error handling be improved when dealing with authentication failures in IMAP_Open?

When dealing with authentication failures in IMAP_Open, error handling can be improved by using try-catch blocks to catch exceptions and handle them appropriately. This allows for more specific error messages to be displayed to the user or logged for debugging purposes.

try {
    $imapStream = imap_open($mailbox, $username, $password);
    if (!$imapStream) {
        throw new Exception('Failed to authenticate with IMAP server');
    }
} catch (Exception $e) {
    echo 'Error: ' . $e->getMessage();
}