Niet the Dark Absol Niet the Dark Absol - 1 year ago 112
PHP Question

imap_check throwing notice when mailbox is empty

So I've recently started my first real job (yay!) and I'm working on an email checker.

It's working great, no errors... provided there are emails.

$mbox = imap_open("{.../pop3/novalidate-cert}INBOX","","...");
$inbox = imap_check($mbox);

The above code works just fine when there are emails in the inbox, but if there aren't I get this error at the end of the page:

Notice: Unknown: Mailbox is empty (errflg=1) in Unknown on line 0

No amount of error suppression seems to be able to stop this from being thrown, other than
, which I'd rather not use (for once!)

Can anything be done?

Answer Source

I think this may be a PHP discrepancy.

Check out this IMAP library I have used this in a project that has imported thousands of emails with no issues.

protected function initImapStream() {
    $imapStream = @imap_open($this->imapPath, $this->login, $this->password);
    if(!$imapStream) {
        throw new ImapMailboxException('Connection error: ' . imap_last_error());
    return $imapStream;

The @ error suppression operator is used, I guess that this is the workaround.


Edit: Turns out you can turn this notice off via an option. Quote from

you can avoid this message :

Warning: (null)(); Mailbox is empty (errflg=1) in Unknown on line 0

by specified the option OP_SILENT to imap_open.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download