i think it will save someones time to get mailbox list and their attributes within a formatted array
/********** Function ******************/
function getAttributesFlagArray($p_attributes){
$attrs[LATT_HASNOCHILDREN]='false';
$attrs[LATT_HASCHILDREN]='false';
$attrs[LATT_REFERRAL]='false';
$attrs[LATT_UNMARKED]='false';
$attrs[LATT_MARKED]='false';
$attrs[LATT_NOSELECT]='false';
$attrs[LATT_NOINFERIORS]='false';
$attrsX=$attrs;
foreach($attrs as $attrkey=>$attrval){
if ($p_attributes & $attrkey){
$attrsX[$attrkey]='true';
$p_attributes-=$attrkey;
}
}
return $attrsX;
}
/*********** Usages *****************/
$mboxconnstr='{imap.gmail.com:993/imap/ssl}';
// $mboxconnstr='{mail.bdways.com:143/imap/novalidate-cert}';
$mbox = imap_open($mboxconnstr, $username, $password, OP_HALFOPEN) or die("can't connect: " . imap_last_error());
echo "<h1>Mailboxes</h1>\n";
$folders = imap_getmailboxes($mbox, $mboxconnstr, "*");
if(is_array($folders)){
foreach($folders as $fkey=>$folder){
$mapname = str_replace($mboxconnstr, "", imap_utf7_decode($folder->name));
if($mapname[0] != ".") {
//$attrs[LATT_]=': NO';
$list_folders[$fkey]['name'] = $folder->name;
$list_folders[$fkey]['nameX'] = $mapname;
$list_folders[$fkey]['delimiter'] = $folder->delimiter;
$list_folders[$fkey]['attributes'] = $folder->attributes;
$list_folders[$fkey]['attr_values'] = getAttributesFlagArray($folder->attributes);
}
}
}else echo "Call failed<br />\n";
echo('<pre>'); print_r($list_folders); echo('</pre>');
thanks to Mohamed Abbas mabbas_xyz at yahoo dot com for his attributes findings using imap...
imap_getmailboxes
(PHP 4, PHP 5)
imap_getmailboxes — 메일박스의 목록을 읽어오고, 각 메일박스에 대한 상세한 정보를 돌려준다.
Description
메일박스 정보를 포함하는 객체의 배열을 돌려준다. 각 객체는 다음 속성을 갖는다. 메일박스의 완전한 이름을 갖는 name 속성; 메일 박스안에 포함된 분류체계(hierarchy)의 부분을 위한 분류체계 한정자(hirerarchy delimiter) delimiter 속성; 그리고 attributes . Attributes 속성은 다음에 대해서 테스트될 수 있는 비트마스크(bitmask)값이다:
- 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.
메일 박스 이름이 출력가능한 아스키(ASCII)코드 범위 밖의 국제적인 문자를 포함한다면 인코딩해야 할것이고, imap_utf7_decode()함수로 디코딩해야 할것이다.
ref 인수는 imap_open()함수 에서와 같이 서버의 사양이면 된다.그리고 pattern 인수는 검색을 시작할 메일 박스의 위치이다. 모든 메일 박스에 적용시키려면 pattern 인수에 '*'라고 하면 된다.
pattern 인수로 쓸 수 있는 두개의 특별한 문자들이 있다 : '*' 과 '%'문자이다. '*'은 모든 메일박스를 돌려줌을 의미한다. pattern 인수에 '*'를 쓰면 모든 메일 박스 체계(hierarchy) 의 목록을 얻을 수 있다. '%'는 현재 레벨만을 돌려준다는 걸 의미한다. pattern 인수에 '%'를 쓰면 최상위 레벨의 메일박스 만을 돌려줄 것이다; UW_IMAPD에 관해 '~/mail/%'을 쓰면 ~/mail 디렉토리의 모든 메일 박스를 돌려주지만, 하위디렉토리는 포함되지 않는다.
Example#1 imap_getmailboxes() example
$mbox = imap_open("{your.imap.host}","username","password",OP_HALFOPEN)
|| die("can't connect: ".imap_last_error());
$list = imap_getmailboxes($mbox,"{your.imap.host}","*");
if(is_array($list)) {
reset($list);
while (list($key, $val) = each($list))
{
print "($key) ";
print imap_utf7_decode($val->name).",";
print "'".$val->delimiter."',";
print $val->attributes."<br>\n";
}
} else
print "imap_getmailboxes failed: ".imap_last_error()."\n";
imap_close($mbox);
See also imap_getsubscribed().
imap_getmailboxes
06-Jan-2008 12:49
25-Jul-2007 01:07
Just to simplify what Mohamed Abbas said to get right to the heart of the matter:
You have 7 constants to test against:
LATT_NOINFERIORS, LATT_NOSELECT, LATT_MARKED, LATT_UNMARKED, LATT_REFERRAL, LATT_HASCHILDREN, and LATT_HASNOCHILDREN.
You do a bitmask test against attributes to test the value; in in PHPese:
if ($val->attributes & LATT_HASNOCHILDREN) {
echo 'this has children';
}
Don't make it more complicated than you have to.
18-Nov-2006 08:51
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
04-Feb-2006 07: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>";
}
}
04-May-2004 01: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
27-Aug-2001 05: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.
