Move IMAP Messages with OAuth2 and the MOVE Extension
See more IMAP Examples
Move matching messages between two existing mailboxes using QueryMbx and MoveMessages. Gmail OAuth2 supplies the concrete connection example. Other servers require their own authentication settings.
This changes all matching messages in the source folder. Choose the source, destination, and search criterion before running it. Gmail custom folders represent labels; moving between them changes label membership. The code checks MOVE at runtime, handles an empty search, and uses UID-based message sets. It does not fall back to copy/delete/EXPUNGE or print authentication logs. Confirm the result before retrying after a lost connection.
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.
Chilkat CkPerl Downloads
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;
}
$capabilities = $imap->capability();
if (($imap->get_LastMethodSuccess() != 1) or ($imap->HasCapability("MOVE",$capabilities) != 1)) {
$imap->Logout();
$imap->Disconnect();
print "This example requires the MOVE capability." . "\r\n";
exit;
}
# These existing custom-label folders are inputs. The operation moves matching messages.
$sourceFolder = "Example";
$destinationFolder = "Example/Processed";
$success = $imap->SelectMailbox($sourceFolder);
if ($success != 1) {
$imap->Logout();
$imap->Disconnect();
print "Could not select the source folder." . "\r\n";
exit;
}
$selected = chilkat::CkMessageSet->new();
$success = $imap->QueryMbx('FROM sender@example.net',1,$selected);
if ($success != 1) {
$imap->Logout();
$imap->Disconnect();
print "Could not search the source folder." . "\r\n";
exit;
}
if ($selected->get_Count() == 0) {
$imap->Logout();
$imap->Disconnect();
print "No matching messages; nothing was moved." . "\r\n";
exit;
}
print "Moving UID set: " . $selected->toCompactString() . "\r\n";
$success = $imap->MoveMessages($selected,$destinationFolder);
if ($success != 1) {
$imap->Logout();
$imap->Disconnect();
print "Move failed. Verify the outcome before retrying an interrupted operation." . "\r\n";
exit;
}
print "MOVE completed for the selected messages." . "\r\n";
$imap->Logout();
$imap->Disconnect();