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

Gmail IMAP: Workspace Service Account with a JSON Key

Use a service-account JSON key with Google Workspace domain-wide delegation. A Workspace administrator must authorize the service account's numeric OAuth client ID for https://mail.google.com/. Set SubEmailAddress to the Workspace user whose active Gmail mailbox is being accessed, and use that same address for IMAP login.

A service account has no Gmail mailbox of its own; leaving the impersonated user empty does not grant access to a personal Gmail account. The signed JWT exchange yields a short-lived token without a refresh token. Obtain another when needed, protect the private key, and keep the computer clock accurate.

This example lists visible folders and examines INBOX read-only. For the different visibility rules of the IMAP-only administrative scope, see gmail.imap_admin delegation.

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

Reference documentation

Chilkat PHP Downloads

PHP
<?php

use Chilkat2\AuthGoogle;
use Chilkat2\Imap;
use Chilkat2\JsonObject;
use Chilkat2\Mailboxes;
use Chilkat2\Socket;

$success = false;

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

$mailbox = 'user@example.com';
$key = new JsonObject();
try {
    $key->LoadFile('gmail-service-account.json');
} catch (\Chilkat2\Exception $e) {
    echo "Could not load the private service-account key.\n";
    exit;
}

if ((!$key->StringOfEquals('type', 'service_account', true)) || ($key->JsonTypeOf('private_key') != 1)) {
    echo "Expected a Google service-account JSON key.\n";
    exit;
}

$auth = new AuthGoogle();
$auth->JsonKey = $key->Emit();
$auth->SubEmailAddress = $mailbox;
$auth->Scope = 'https://mail.google.com/';
$auth->ExpireNumSeconds = 3600;
$connection = new Socket();
$connection->RequireSslCertVerify = true;
try {
    $connection->Connect('www.googleapis.com', 443, true, 10000);
} catch (\Chilkat2\Exception $e) {
    echo "Could not establish the Google token connection.\n";
    exit;
}

try {
    $auth->ObtainAccessToken($connection);
    $success = true;
} catch (\Chilkat2\Exception $e) {
    $success = false;
}
$connection->Close(1000);
if (!$success) {
    echo "Check the key, domain-wide delegation client ID/scope, user, and computer clock.\n";
    exit;
}

$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, $auth->AccessToken);
} 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 the labels visible through ordinary full-mail IMAP access.
$i = 0;
while ($i < $folders->Count) {
    echo $folders->GetName($i), "\n";
    $i = $i + 1;
}

try {
    $imap->ExamineMailbox('INBOX');
} catch (\Chilkat2\Exception $e) {
    $imap->Logout();
    $imap->Disconnect();
    echo "Could not examine the impersonated user's INBOX.\n";
    exit;
}

echo 'INBOX messages: ', $imap->NumMessages, "\n";
$imap->Logout();
$imap->Disconnect();