Sample code for 30+ languages & platforms
CkPerl Requires Chilkat v11.0.0+

Create an IMAP Folder: Gmail Labels with OAuth2

Create a custom Gmail label and a nested label using CreateMailbox. The example lists existing mailboxes first, skips names already present, and uses the hierarchy separator returned by LIST.

This creates labels in the account. A top-level name is not automatically a child of INBOX. In Gmail, IMAP mailboxes represent labels; creating a label does not apply it to a message. Chilkat handles Unicode mailbox names in its mailbox methods. If creating the child fails, the parent remains. A concurrent creation can require listing again.

First authorize the mailbox for https://mail.google.com/ and refresh the token as needed. Read the current token from gmail-mail-token.json in a private working directory. Narrow Gmail REST scopes do not authorize these protocols. Keep the token and authentication logs private.

See the Gmail SMTP, IMAP, and POP3 guide for current authentication choices and Gmail-specific examples.

Reference documentation

Chilkat CkPerl Downloads

CkPerl
use chilkat();

$success = 0;

#  Assumes Chilkat is unlocked. See Global Unlock Sample.

$token = chilkat::CkJsonObject->new();
$success = $token->LoadFile("gmail-mail-token.json");
if ($success != 1) {
    print "Could not read gmail-mail-token.json from the private working directory." . "\r\n";
    exit;
}

if (($token->JsonTypeOf("access_token") != 1) or ($token->StringOfEquals("access_token","",1) == 1)) {
    print "An OAuth access token is required. Authorize or refresh the account first." . "\r\n";
    exit;
}

$mailbox = 'user@example.com';
$imap = chilkat::CkImap->new();
$imap->put_Ssl(1);
$imap->put_Port(993);
$imap->put_RequireSslCertVerify(1);
$imap->put_AuthMethod("XOAUTH2");
$success = $imap->Connect("imap.gmail.com");
if ($success != 1) {
    print "Could not establish the Gmail IMAP TLS connection." . "\r\n";
    exit;
}

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

#  This creates a custom Gmail label and one nested label.
$parent = "Example";
$folders = chilkat::CkMailboxes->new();
$success = $imap->MbxList(0,"","*",$folders);
if ($success != 1) {
    $imap->Logout();
    $imap->Disconnect();
    print "Could not list existing labels." . "\r\n";
    exit;
}

$separator = $imap->separatorChar();
if ($separator == "") {
    $imap->Logout();
    $imap->Disconnect();
    print "The server did not provide a hierarchy separator." . "\r\n";
    exit;
}

if ($folders->GetMailboxIndex($parent) < 0) {
    $success = $imap->CreateMailbox($parent);
    if ($success != 1) {
        $imap->Logout();
        $imap->Disconnect();
        print "Could not create the parent label." . "\r\n";
        exit;
    }

}

$child = chilkat::CkStringBuilder->new();
$child->Append($parent);
$child->Append($separator);
$child->Append("Child");
if ($folders->GetMailboxIndex($child->getAsString()) < 0) {
    $success = $imap->CreateMailbox($child->getAsString());
    if ($success != 1) {
        $imap->Logout();
        $imap->Disconnect();
        print "Could not create the child label. The parent may already exist." . "\r\n";
        exit;
    }

}

print "Labels available: " . $parent . " and " . $child->getAsString() . "\r\n";
$imap->Logout();
$imap->Disconnect();