imap_getmailboxes

(PHP 3 >= 3.0.12, PHP 4, PHP 5)

imap_getmailboxes --  Read the list of mailboxes, returning detailed information on each one

Description

array imap_getmailboxes ( resource imap_stream, string ref, string pattern )

Returns an array of objects containing mailbox information. Each object has the attributes name, specifying the full name of the mailbox; delimiter, which is the hierarchy delimiter for the part of the hierarchy this mailbox is in; and attributes. Attributes is a bitmask that can be tested against:

  • LATT_NOINFERIORS - This mailbox has no "children" (there are no mailboxes below this one).

  • LATT_NOSELECT - This is only a container, not a mailbox - you cannot open it.

  • LATT_MARKED - This mailbox is marked. Only used by UW-IMAPD.

  • LATT_UNMARKED - This mailbox is not marked. Only used by UW-IMAPD.

Mailbox names containing international Characters outside the printable ASCII range will be encoded and may be decoded by imap_utf7_decode().

ref should normally be just the server specification as described in imap_open(), and pattern specifies where in the mailbox hierarchy to start searching. If you want all mailboxes, pass '*' for pattern.

There are two special characters you can pass as part of the pattern: '*' and '%'. '*' means to return all mailboxes. If you pass pattern as '*', you will get a list of the entire mailbox hierarchy. '%' means to return the current level only. '%' as the pattern parameter will return only the top level mailboxes; '~/mail/%' on UW_IMAPD will return every mailbox in the ~/mail directory, but none in subfolders of that directory.

例子 1. imap_getmailboxes() example

<?php
$mbox
= imap_open("{your.imap.host}", "username", "password", OP_HALFOPEN)
      or die(
"can't connect: " . imap_last_error());

$list = imap_getmailboxes($mbox, "{your.imap.host}", "*");
if (
is_array($list)) {
    foreach (
$list as $key => $val) {
        echo
"($key) ";
        echo
imap_utf7_decode($val->name) . ",";
        echo
"'" . $val->delimiter . "',";
        echo
$val->attributes . "<br />\n";
    }
} else {
    echo
"imap_getmailboxes failed: " . imap_last_error() . "\n";
}

imap_close($mbox);
?>

See also imap_getsubscribed().


add a note add a note User Contributed Notes
emielbom at hotmail dot com
04-Feb-2006 11:09
i was looking for a function to test the attributes.
and it was hard to find an answer or code.

Maybe there is a function availeble, but i couldn't find it.

the first function: returns all mailboxes in on the server

The second function: returns a bitstring from the attr_number returned by getmailboxes()

The tirth function: returns an array with the active attributes on the specific mailbox

Last Line: is a combination of the functions: the output is, any map witch can hold no mail but only sub boxes...

function getFolders($mailbox, $serverString) {

   $list = imap_getmailboxes($mailbox, $serverString, "*");
   if (is_array($list)) {
       foreach ($list as $key => $val) {
           $mapname = str_replace($serverString, "", imap_utf7_decode($val->name));
           if ($mapname[0] != ".") {
               $list_folders[$key]['name'] = $mapname;
               $list_folders[$key]['delimiter'] = $val->delimiter;
               $list_folders[$key]['attributes'] = $val->attributes;
           }
          
       }
   } else {
       echo "imap_getmailboxes failed: " . imap_last_error() . "\n";
   }

   return $list_folders;
}

function IntToBin($number) {
   $BitWaarde = 1;
   $IntNum = $number;
   $BinString = "";

   if ($IntNum > 0) {
    
       // bepaal de max bitwaarde aan de hand van $IntNum
       while ($IntNum > $BitWaarde) {
           $BitWaarde = $BitWaarde * 2;
       }
    
    
    
       // maken van een binaire string.
       while ($BitWaarde >= 1 ) {
           if ($IntNum < $BitWaarde) {
               if ($BinString != "") $BinString .= "0";
           } else {
               $BinString .= "1";
               $IntNum = $IntNum-$BitWaarde;
           }
           $BitWaarde = $BitWaarde / 2;
       }
  
   }
  
  
   return $BinString;
}

function Attributes($BinString) {

   $BinInt = (int)$BinString;
   if ($BinInt >=1000){
       $setAttribute['LATT_UNMARKED'] = true;
       $BinInt = $BinInt-1000;
   } else $setAttribute['LATT_UNMARKED'] = false;

   if ($BinInt >=100){
       $setAttribute['LATT_MARKED'] = true;
       $BinInt = $BinInt-100;
   } else $setAttribute['LATT_MARKED'] = false;
  
   if ($BinInt >=10){
       $setAttribute['LATT_NOSELECT'] = true;
       $BinInt = $BinInt-10;
   } else $setAttribute['LATT_NOSELECT'] = false;
  
   if ($BinInt >=1){
       $setAttribute['LATT_NOINFERIORS'] = true;
       $BinInt = $BinInt-1;
   } else $setAttribute['LATT_NOINFERIORS'] = false;

   return $setAttribute;
}

foreach (getFolders($mailbox, $config['Server_string']) as $key => $val) {
                       $Attr = Attributes(IntToBin((int)$val['attributes']));
                       if (!$Attr['LATT_NOINFERIORS']) {
                           echo "<option value='".$val['name']."'>".$val['name']."</option>";
                       }
                   }
ad-rotator.com
05-May-2004 04:17
In case you print_r() or var_dump() the object and see an int for attribute, these are the constant integers for the bitmask.

1 LATT_NOINFERIORS
2 LATT_NOSELECT
4 LATT_MARKED
8 LATT_UNMARKED
foom at fuhm dot NO_supercalifragilisticexpialidocious_SPAM dot net
28-Aug-2001 08:42
The list of mailbox attributes in this document is very misleading. In particular the explanation of noinferiors is just wrong. It does not mean that the mailbox currently has no children, it means that it *cannot* have children ever. Also, it is untrue that marked and unmarked are only used by UW-IMAP. They are in the official IMAP specification and are used by at least Courier-imap as well.

One thing to watch out for, however, is broken IMAP servers which do send \Noinferiors when they mean that there are currently no children.

From the IMAP4rev1 specs (RFC 2060):
     \Noinferiors

         It is not possible for any child levels of hierarchy to exist
         under this name; no child levels exist now and none can be
         created in the future.

     \Noselect

         It is not possible to use this name as a selectable mailbox.

     \Marked

         The mailbox has been marked "interesting" by the server; the
         mailbox probably contains messages that have been added since
         the last time the mailbox was selected.

     \Unmarked

         The mailbox does not contain any additional messages since the
         last time the mailbox was selected.
ROlsthoorn at mindport dot com
10-Nov-1999 03:48
I have tried the same thing with RedHat 6.1 (Apache 1.3.6/imap 4.5/mod_php3-3.0.8) to my local mail server. The same error: Fatal error: Call to unsupported or undefined function imap_getmailboxes() in occurred: ...

There is no symbol imap_getmailboxes in the libphp3.so