POV
POV

Reputation: 12005

How to get full size of mailbox using IMAP?

I use this library PHP for IMAP connections.

How to get full size of mailbox using IMAP?

I tried this working code, but it only returns inbox directory's size:

var_dump($imap->getMailboxStatistics());

Upvotes: 4

Views: 2007

Answers (2)

InterLinked
InterLinked

Reputation: 1403

The accepted answer was true when it was posted, but it's not necessarily true anymore.

RFC 8438, published in 2018, provides the STATUS=SIZE extension, which allows the mailbox size to be requested in the STATUS command, e.g.

1 STATUS "INBOX" (SIZE)
* STATUS (SIZE 49463)
1 OK STATUS complete

Even nicer, if the server also supports LIST-EXTENDED and LIST-STATUS, you can get this in the LIST STATUS response by requesting it as the SIZE item. A single roundtrip for something that could otherwise involve 2n+1 or 3n+1 RTTs if you're not pipelining.

Unfortunately, very few IMAP servers support this extension; you can bet no commercial provider will support it. In that case, you'll have to fall back to a FETCH 1:* (RFC822.SIZE). Apart from being wasteful, this can take an extremely long time (think 10 seconds or more) on large mailboxes (with tens of thousands of messages). The sheer size of the response can also be large: several MBs. If you do this, I would also advise caching this value, if possible, and using the STATUS response to invalidate the cache. That way, you don't need to recompute the size if the mailbox has not changed. There are further optimizations you can make (like incremental FETCH) discussed in this code snippet that computes the size of a remote IMAP folder when STATUS=SIZE is not available.

Upvotes: 1

Jan Kundrát
Jan Kundrát

Reputation: 3816

There is no standard way of obtaining the total size of a mailbox in IMAP. RFC 3501 does not include a command for that, and there is also no extension which provides such a functionality.

Your code could probably issue something like FETCH 1:* (RFC822.SIZE) and sum it up locally. That is quite a wasteful approach, unfortunately.

Upvotes: 5

Related Questions