List all folders in the mailbox
suggest changeAfter imap_open, imap_list lists mailbox names. The first argument is the connection, the second is the server reference, and the third is a mailbox pattern. * matches across hierarchy levels. % matches only inside the current level. /secure in the sample reference is not a documented PHP IMAP flag. Typical flags are /ssl and /tls.
$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.
Found a mistake? Have a question or improvement idea?
Let me know.
Table Of Contents