If you do not use imap_errors() to clear the error stack, any errors that remain at the end of the script execution will be raised as PHP Notices.
(PHP 4, PHP 5, PHP 7, PHP 8)
imap_errors — Retorna todos os erros IMAP que ocorreram
Obtém todos os erros IMAP (se ocorreram) durante esta solicitação de página ou desde que a pilha de erros foi redefinida.
Quando imap_errors() é chamada, a pilha de erros é posteriormente limpa.
Esta função não possui parâmetros.
Esta função retorna um array de todas as mensagens de erro IMAP
geradas desde a última chamada de imap_errors(),
ou desde o início da página. Retorna false
se nenhuma mensagem de erro estiver
disponível.
If you do not use imap_errors() to clear the error stack, any errors that remain at the end of the script execution will be raised as PHP Notices.
When calling imap_close($mbox), notices will be generated for each error that has occurred within the imap functions. To suppress these error messages (including Mailbox is empty, which is not really an error) simply call imap_errors() and then imap_close($mbox).
For those curious, this function will return a linear array of strings as opposed to say error_get_last which returns an associative array of different things.
e.g.
[0 => '[TRYCREATE] No folder {imap.gmail.com} (Failure)']
This can generate the string "Mailbox is empty" right after a call to imap_open(). That's not an error. That means something like this is not good enough to know the open failed due to a wrong password or host name or whatever. This
$imap = @imap_open( $mailbox, $user, $pass);
$errors = @imap_errors();
if ( $errors ) {
echo 'Login failed: ' . implode ('; ', $errors );
}
can output "Login failed: Mailbox is empty" which is silly.
Instead, check the return value from imap_open().
$imap = @imap_open( $mailbox, $user, $pass);
if ( ! $imap ) {
$errors = @imap_errors();
echo 'Login failed: ' . implode ('; ', $errors );
}