PHP 8.3.4 Released!

imap_getmailboxes

(PHP 4, PHP 5, PHP 7, PHP 8)

imap_getmailboxesLiefert detaillierte Informationen zu allen Postfächern

Beschreibung

imap_getmailboxes(IMAP\Connection $imap, string $reference, string $pattern): array|false

Gibt Informationen über die Postfächer zurück.

Parameter-Liste

imap

Eine IMAP\Connection-Instanz.

reference

Die Angabe des Servers in der in imap_open() beschriebenen Form.

Warnung

Die Übergabe von nicht vertrauenswürdigen Daten an diesen Parameter ist unsicher,falls imap.enable_insecure_rsh nicht deaktiviert ist.

pattern

Gibt an, wo in der Postfach-Hierarchie mit der Suche begonnen werden soll.

Es gibt zwei Sonderzeichen, die man als Teil von pattern verwenden kann: '*' und '%'. '*' heißt, dass alle Postfächer zurückgegeben werden sollen. Wenn man pattern als '*' übergibt, so erhält man eine Liste der vollständigen Postfach-Hierarchie. '%' heißt, dass nur die aktuelle Stufe zurückgegeben wird. '%' als Wert des Parameters pattern gibt nur die Postfächer der obersten Ebene zurück. '~/mail/%' unter UW_IMAPD gibt alle Postfächer des Verzeichnisses ~/mail zurück, aber keine aus dessen Unterverzeichnissen.

Rückgabewerte

Gibt ein Array zurück, das Objekte mit Postfachbeschreibungen enthält. Jedes Objekt verfügt über die Attribute name für den Namen des Postfachs, delimiter für das gültige Hierachietrennzeichen innerhalb des Hierachieabschnitts, in dem sich das Postfach befindet, und den Eigenschaften des Postfachs in attributes. Die Eigenschaften setzen sich als Bitmaske aus folgenden möglichen Werten zusammen:

  • LATT_NOINFERIORS - Dieses Postfach enthält keine Unterpostfächer und kann auch keine enthalten. Der Aufruf von imap_createmailbox() wird für dieses Postfach fehlschlagen.

  • LATT_NOSELECT - Dieses Postfach enthält nur Unterpostfächer aber keine Nachrichten; es kann nicht geöffnet werden.

  • LATT_MARKED - Dieses Postfach ist markiert. Das bedeutet, dass es neue Nachrichten enthalten kann, seitdem es zuletzt geprüft wurde. Wird nicht von allen IMAP-Servern unterstützt.

  • LATT_UNMARKED - Dieses Postfach ist nicht markiert, d. h. es enthält keine neuen Nachrichten. Wenn entweder MARKED oder UNMARKED vorhanden ist, kann man davon ausgehen, das der IMAP-Server dieses Feature für dieses Postfach unterstützt.

  • LATT_REFERRAL - Dieses Postfach bezieht sich auf eine Remote-Mailbox.

  • LATT_HASCHILDREN - Dieses Postfach enthält auswählbare Unterpostfächer.

  • LATT_HASNOCHILDREN - Dieses Postfach enthält keine auswählbaren Unterpostfächer.

Diese Funktion gibt bei einem Fehler false zurück.

Changelog

Version Beschreibung
8.1.0 Der Parameter imap erwartet nun eine IMAP\Connection-Instanz; vorher wurde eine gültige imap-Ressource erwartet.

Beispiele

Beispiel #1 imap_getmailboxes()-Beispiel

<?php
$mbox
= imap_open("{imap.example.org}", "username", "password", OP_HALFOPEN)
or die(
"Kann keine Verbindung aufbauen: " . imap_last_error());

$list = imap_getmailboxes($mbox, "{imap.example.org}", "*");
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 schlug fehl: " . imap_last_error() . "\n";
}

imap_close($mbox);
?>

Siehe auch

add a note

User Contributed Notes 5 notes

up
13
Mohamed Abbas mabbas_xyz at yahoo dot com
17 years ago
i am currently develop a simple IMAP client, when i call imap_getmailboxes() i receive a different values in attributes property of the mailbox object the problem is how can i manipulate these attributes to get a meaningful value,
if you make a hard search to find a solution for this, you will
not find any useful documents for this problem, let us take a closer look for this problem.

when i call imap_getmailboxes() against different IMAP servers i got these attribute values

[attributes] => 9
[attributes] => 1
[attributes] => 64
[attributes] => 32
[attributes] => 40

the documentation tell us that we check this attributes against four constants, these contents are

LATT_NOINFERIORS
LATT_NOSELECT
LATT_MARKED
LATT_UNMARKED

the value of these constants are

LATT_NOINFERIORS = 1
LATT_NOSELECT = 2
LATT_MARKED = 4
LATT_UNMARKED = 8

you can got this result by echo each constant, unfortunately the documentation not explain how we can check the attributes against the constants, after a long time of searching i find the answer in source code of c-client
(you can get the source from ftp://ftp.cac.washington.edu/imap/)
under \src\c-client you will find mail.h open it and you will find this

/* terminal node in hierarchy */
#define LATT_NOINFERIORS (long) 0x1
/* name can not be selected */
#define LATT_NOSELECT (long) 0x2
/* changed since last accessed */
#define LATT_MARKED (long) 0x4
/* accessed since last changed */
#define LATT_UNMARKED (long) 0x8
/* name has referral to remote mailbox */
#define LATT_REFERRAL (long) 0x10
/* has selectable inferiors */
#define LATT_HASCHILDREN (long) 0x20
/* has no selectable inferiors */
#define LATT_HASNOCHILDREN (long) 0x40

as you notice here these are our four constants and three additional constants

LATT_REFERRAL
LATT_HASCHILDREN
LATT_HASNOCHILDREN

then what is the value of these 3 attributes
LATT_REFERRAL 0x10 = 00010000 in binary, the bitmask value is 2^4 = 16 and so on, or simply echo this constant to get the value, then

LATT_REFERRAL = 16
LATT_HASCHILDREN = 32
LATT_HASNOCHILDREN = 64

finally the full list of constants will be
LATT_NOINFERIORS = 1
LATT_NOSELECT = 2
LATT_MARKED = 4
LATT_UNMARKED = 8
LATT_REFERRAL = 16
LATT_HASCHILDREN = 32
LATT_HASNOCHILDREN = 64

ok let's back to our attributes
[attributes] => 9
[attributes] => 1
[attributes] => 64
[attributes] => 32
[attributes] => 40

[attributes] => 9 this mean it's LATT_UNMARKED and LATT_NOINFERIORS 1+8 =9

[attributes] => 1 this mean LATT_NOINFERIORS

[attributes] => 64 this mean LATT_HASNOCHILDREN

[attributes] => 32 this mean LATT_HASCHILDREN

[attributes] => 40 this mean LATT_HASCHILDREN and LATT_UNMARKED 32+8=40

this just like linux file permission 7 mean read, write, and execute 4+2+1 read=4 write=2 execute=1

that is what i found, i hope this can help

Mohamed Abbas
Nileweb Egypt
up
5
foom at fuhm dot NO_supercalifragilisticexpialidocious_SPAM dot net
22 years ago
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.
up
1
ad-rotator.com
19 years ago
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
up
-1
emielbom at hotmail dot com
18 years ago
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...

<?php
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>";
}
}
?>
up
-3
james dot mattingly at gmail dot com
12 years ago
For the purposes of passing along the attributes as a json array, I wanted to pass the attributes as an indexed array to save on bandwidth and session storage. The human readable translation is then performed in javascript by relating each key to the proper attribute label. I also wanted a cleaner way of converting the attributes without calling another function or using a complex iteration. This puts each binary attribute into it's own array element for easy testing.

<?php
$mailboxes
= imap_getmailboxes($mailbox, $username, $password);

foreach(
$mailboxes as $key=>$mailbox){
...
$attrs = preg_split('//',str_pad(decbin($mailbox->attributes),7,"0",STR_PAD_LEFT),8); // convert decimal to an array of attributes
array_shift($attrs); // Remove the extra element at the front of the array in position 0
...
}
?>

With temporary variables, the conversion would look like this.

<?php
$attr_binary
= decbin($mailbox->attributes); // convert decimal to binary
$attr_binary = str_pad($attr_binary,7,"0",STR_PAD_LEFT); // pad with 0's on the left
$attrs = $preg_split('//',$attr_binary,8); // split into an array after each character
array_shift($attrs); // Remove the extra element at the front of the array in position 0
?>

By defining the attributes as a map to the array index, you can test as follows.

<?php
define
("ATTR_NOINFERIORS",6);
define("ATTR_NOSELECT "5);
define("ATTR_MARKED ",4);
define("ATTR_UNMARKED ",3);
define("ATTR_REFERRAL ",2);
define("ATTR_HASCHILDREN ",1);
define("ATTR_HASNOCHILDREN ",0);

if (
$attrs[ATTR_HASCHILDREN]){
// do something
}
?>
To Top