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:


IMAP:
* IMAP
* List all folders in the mailbox

Table Of Contents
2 Arrays
4 Types
10 Cookies
14 JSON
15 SOAP
17 cURL
19 XML
21 Traits
35 UTF-8
36 URLs
38 PHPDoc
41 Loops
44 Closur
72 YAML
77 Cache
78 Streams
81 PDO
82 SQLite3
83 Sockets
87 MongoDB
93 IMAP
94 Redis
95 Imagick
102 APCu
108 PSR