Sample code for 30+ languages & platforms
PHP ActiveX Requires Chilkat v11.1.0+

Gmail IMAP: Discover Special Folders

See more GMail SMTP/IMAP/POP Examples

Find Gmail special folders from the attributes returned by LIST, using MbxList and Mailboxes.HasFlag. The result maps roles such as Sent and Trash to the server-provided mailbox names. Chilkat decodes mailbox names for use with its mailbox methods.

Folder names can be localized and visibility can differ by account. The role attributes are stable; do not hard-code [Gmail]/Sent Mail or use the deprecated XLIST command. INBOX is a protocol-defined name. Missing map keys indicate folders not advertised by the server.

Use the mailbox that authorized the full-mail scope https://mail.google.com/. Obtain the protocol OAuth token and refresh it before running this example. The token is loaded from gmail-mail-token.json in a private working directory; a narrow Gmail REST token is insufficient.

Google documentation

Chilkat PHP ActiveX Downloads

PHP ActiveX
<?php

$success = 0;

//  Assumes Chilkat is unlocked. See Global Unlock Sample.

$token = new COM("Chilkat.JsonObject");
$success = $token->LoadFile('gmail-mail-token.json');
if ($success != 1) {
    print 'Could not load the protocol token from the private working directory.' . "\n";
    exit;
}

if (($token->JsonTypeOf('access_token') != 1) or ($token->StringOfEquals('access_token','',1) == 1)) {
    print 'Authorize or refresh the mailbox first.' . "\n";
    exit;
}

$mailbox = 'user@example.com';
$imap = new COM("Chilkat.Imap");
$imap->Ssl = 1;
$imap->Port = 993;
$imap->RequireSslCertVerify = 1;
$imap->AuthMethod = 'XOAUTH2';
$success = $imap->Connect('imap.gmail.com');
if ($success != 1) {
    print 'Could not establish the Gmail IMAP TLS connection.' . "\n";
    exit;
}

$success = $imap->Login($mailbox,$token->stringOf('access_token'));
if ($success != 1) {
    $imap->Disconnect();
    print 'IMAP authentication failed. Check the mailbox, scope, expiry, and account policy.' . "\n";
    exit;
}

$folders = new COM("Chilkat.Mailboxes");
$success = $imap->MbxList(0,'','*',$folders);
if ($success != 1) {
    $imap->Logout();
    $imap->Disconnect();
    print 'Could not list Gmail folders.' . "\n";
    exit;
}

//  LIST special-use attributes work with localized folder names. XLIST is obsolete.
$special = new COM("Chilkat.JsonObject");
$i = 0;
while (($i < $folders->Count)) {
    if ($folders->IsSelectable($i) == 1) {
        print $folders->getName($i) . '  ' . $folders->getFlags($i) . "\n";
        if ($folders->HasFlag($i,'\\All') == 1) {
            $special->UpdateString('allMail',$folders->getName($i));
        }

        if ($folders->HasFlag($i,'\\Sent') == 1) {
            $special->UpdateString('sent',$folders->getName($i));
        }

        if ($folders->HasFlag($i,'\\Drafts') == 1) {
            $special->UpdateString('drafts',$folders->getName($i));
        }

        if ($folders->HasFlag($i,'\\Junk') == 1) {
            $special->UpdateString('spam',$folders->getName($i));
        }

        if ($folders->HasFlag($i,'\\Trash') == 1) {
            $special->UpdateString('trash',$folders->getName($i));
        }

        if ($folders->HasFlag($i,'\\Flagged') == 1) {
            $special->UpdateString('starred',$folders->getName($i));
        }

        if ($folders->HasFlag($i,'\\Important') == 1) {
            $special->UpdateString('important',$folders->getName($i));
        }

    }

    $i = $i + 1;
}

$imap->Logout();
$imap->Disconnect();
$special->EmitCompact = 0;
print $special->emit() . "\n";
//  Missing keys mean the folder was not advertised. Do not substitute an English name.

?>