Skip to content

Core\Imap\Folder::move

$folder->move(int $uid, string $folder): void$folder->move(
    int $uid,
    string $folder,
): void

Moves one message to another folder of the same mailbox, such as Archive.

Pass the UID of the message (the number the server gives each message in a folder) and the name of the folder. The message keeps its flags, so a read message is still read in the new folder. In the new folder, the message gets a new UID, and its old UID no longer works in the old folder. If you need the message again, search for it in the new folder.

Only the message you name is moved. If the target folder does not exist, move throws an IOError.

Good to know: the server must support moving one message by its UID. Every common mail server does. On a server that does not, move throws an IOError, and no message is moved.

The examples below move a message to the archive, show its new UID, and move each bounce to its own folder. Each example moves the messages back at the end, so it prints the same every time.

Parameters

  • $uidintA UID (the number the server gives each message in a folder) that search returned.
  • $folderstringThe name of the folder to move it to, such as Archive.

Return value

Returns void. Nothing.

Errors

  • RuntimeError$uid is less than 1 or larger than 4294967295.
  • IOErrorNo message has that UID, the folder $folder does not exist, the server does not support moving a single message, or the server does not answer.

Changelog

VersionChange
0.0.6Core\Imap reads a mailbox over IMAP (the protocol a program uses to read a mailbox on a server). A program opens an [imap.<name>] block of nvs.toml by its name, and then searches, fetches, moves, flags and deletes messages. For tests, a directory block reads a folder of .eml files instead of a server.

Examples

The examples below run with this configuration file.

nvs.toml
# Each example reads its own mailbox, a folder beside this file. They need no mail server.
[imap.shop]
directory = "shop"
[imap.support]
directory = "support"
[imap.newsletter]
directory = "newsletter"
# The program may open the three blocks.
[capabilities.imap]
connect = ["shop", "support", "newsletter"]

Move a message to the archive

<?nvs
// Shows how to move a message from `INBOX` to `Archive`.
use Core\Imap;
var $mailbox = Imap::open('shop');
var $inbox = $mailbox->folder('INBOX');
var $archive = $mailbox->folder('Archive');
// Message 2 says that an order arrived, so the shop moves it to the archive.
$inbox->move(2, 'Archive');
foreach ($inbox->search() as int $uid) {
echo 'INBOX ', $uid, ': ', $inbox->fetch($uid)->subject(), "\n";
}
foreach ($archive->search() as int $uid) {
echo 'Archive ', $uid, ': ', $archive->fetch($uid)->subject(), "\n";
}
// This moves the message back, so the example prints the same each time it runs.
$archive->move(2, 'INBOX');
Output
INBOX 1: Where is my order?
Archive 1: Order 1020 arrived
Archive 2: Order 1038 arrived

The message gets a new uid

<?nvs
// Shows that a moved message has a new UID in its new folder and keeps its flags.
use Core\Arr;
use Core\Imap;
var $mailbox = Imap::open('support');
var $inbox = $mailbox->folder('INBOX');
var $done = $mailbox->folder('Done');
// Message 3 is read. The script moves it to the folder `Done`.
// `Done` has the UIDs 1 and 2 already, so the moved message gets the UID 3 there.
$inbox->move(3, 'Done');
echo 'Done 3: ', $done->fetch(3)->subject(), "\n";
// The message is still read, so `Done` has no unread message. This prints "unread in Done: 0".
echo 'unread in Done: ', Arr::count($done->search({ unseen: true })), "\n";
// `INBOX` has no message with the old UID now, so `fetch` throws an `IOError`.
try {
$inbox->fetch(3);
} catch (IOError $gone) {
echo $gone->message, "\n";
}
// This moves the message back, so the example prints the same each time it runs.
$done->move(3, 'INBOX');
Output
Done 3: Opening hours
unread in Done: 0
Core\Imap\Folder::fetch: the folder has no message with the UID 3

Move each bounce to its own folder

<?nvs
// Shows a newsletter script that finds the bounces in `INBOX` and moves each one to `Bounces`.
use Core\Imap;
use Core\Mail\Message;
var $mailbox = Imap::open('newsletter');
var $inbox = $mailbox->folder('INBOX');
var $bounces = $mailbox->folder('Bounces');
// A bounce is a report that a mail could not be delivered.
foreach ($inbox->search() as int $uid) {
Message $message = $inbox->fetch($uid);
if ($message->kind() == Message::Kind::Bounce) {
foreach ($message->report()?->recipients() ?? [] as var $recipient) {
echo 'bounce ', $uid, ' for ', $recipient->address(), "\n";
}
$inbox->move($uid, 'Bounces');
}
}
// Only the reply from Ada is left in `INBOX`.
foreach ($inbox->search() as int $uid) {
echo 'INBOX ', $uid, ': ', $inbox->fetch($uid)->subject(), "\n";
}
// These lines move the bounces back, so the example prints the same each time it runs.
foreach ($bounces->search({ unseen: true }) as int $uid) {
$bounces->move($uid, 'INBOX');
}
Output
bounce 2 for bo@example.org
bounce 3 for cy@example.net
INBOX 1: Re: Our offers for November