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.
Chilkat PHP Downloads
<?php
use Chilkat2\Imap;
use Chilkat2\JsonObject;
use Chilkat2\Mailboxes;
// Assumes Chilkat is unlocked. See Global Unlock Sample.
$token = new JsonObject();
try {
$token->LoadFile('gmail-mail-token.json');
} catch (\Chilkat2\Exception $e) {
echo "Could not load the protocol token from the private working directory.\n";
exit;
}
if (($token->JsonTypeOf('access_token') != 1) || ($token->StringOfEquals('access_token', '', true))) {
echo "Authorize or refresh the mailbox first.\n";
exit;
}
$mailbox = 'user@example.com';
$imap = new Imap();
$imap->Ssl = true;
$imap->Port = 993;
$imap->RequireSslCertVerify = true;
$imap->AuthMethod = 'XOAUTH2';
try {
$imap->Connect('imap.gmail.com');
} catch (\Chilkat2\Exception $e) {
echo "Could not establish the Gmail IMAP TLS connection.\n";
exit;
}
try {
$imap->Login($mailbox, $token->StringOf('access_token'));
} catch (\Chilkat2\Exception $e) {
$imap->Disconnect();
echo "IMAP authentication failed. Check the mailbox, scope, expiry, and account policy.\n";
exit;
}
$folders = new Mailboxes();
try {
$imap->MbxList(false, '', '*', $folders);
} catch (\Chilkat2\Exception $e) {
$imap->Logout();
$imap->Disconnect();
echo "Could not list Gmail folders.\n";
exit;
}
// LIST special-use attributes work with localized folder names. XLIST is obsolete.
$special = new JsonObject();
$i = 0;
while ($i < $folders->Count) {
if ($folders->IsSelectable($i)) {
echo $folders->GetName($i), ' ', $folders->GetFlags($i), "\n";
if ($folders->HasFlag($i, '\All')) {
$special->UpdateString('allMail', $folders->GetName($i));
}
if ($folders->HasFlag($i, '\Sent')) {
$special->UpdateString('sent', $folders->GetName($i));
}
if ($folders->HasFlag($i, '\Drafts')) {
$special->UpdateString('drafts', $folders->GetName($i));
}
if ($folders->HasFlag($i, '\Junk')) {
$special->UpdateString('spam', $folders->GetName($i));
}
if ($folders->HasFlag($i, '\Trash')) {
$special->UpdateString('trash', $folders->GetName($i));
}
if ($folders->HasFlag($i, '\Flagged')) {
$special->UpdateString('starred', $folders->GetName($i));
}
if ($folders->HasFlag($i, '\Important')) {
$special->UpdateString('important', $folders->GetName($i));
}
}
$i = $i + 1;
}
$imap->Logout();
$imap->Disconnect();
$special->EmitCompact = false;
echo $special->Emit(), "\n";
// Missing keys mean the folder was not advertised. Do not substitute an English name.