List all folders in the mailbox

suggest change

Once you’ve connected to your mailbox, you’ll want to take a look inside. The first useful command is imap_list. The first parameter is the resource you acquired from imap_open, the second is your mailbox string and the third is a fuzzy search string (\* is used to match any pattern).

$folders = imap_list($mailbox, "{imap.example.com:993/imap/tls/secure}", "*");
if ($folders === false) {
    echo "Failed to list folders in mailbox";
} else {
    print_r($folders);
}

The output should look similar to this

Array
(
    [0] => {imap.example.com:993/imap/tls/secure}INBOX
    [1] => {imap.example.com:993/imap/tls/secure}INBOX.Sent
    [2] => {imap.example.com:993/imap/tls/secure}INBOX.Drafts
    [3] => {imap.example.com:993/imap/tls/secure}INBOX.Junk
    [4] => {imap.example.com:993/imap/tls/secure}INBOX.Trash
)

You can use the third parameter to filter these results like this:

$folders = imap_list($mailbox, "{imap.example.com:993/imap/tls/secure}", "*.Sent");

And now the result only contains entries with .Sent in the name:

Array
(
    [0] => {imap.example.com:993/imap/tls/secure}INBOX.Sent
)

Note: Using \* as a fuzzy search will return all matches recursively. If you use % it will return only matches in the current folder specified.

Feedback about page:

Feedback:
Optional: your email if you want me to get back to you:



Table Of Contents