Content · Web Development · PHP

PHP imap_mail_move() Function: Usage and Examples

Moves message(s) to another IMAP.

1 views Published Oct 17, 2026 Updated Oct 17, 2026 admin

Moves message(s) to another IMAP mailbox. This guide demonstrates its use in PHP code and explains when the result or side effect needs extra checks.

What It Does


Moves message(s) to another IMAP mailbox.

Syntax and Parameters


The examples below show a real call to imap_mail_move with suitable arguments. Read the function call in context: its connection, handle, input data or other prerequisites are created before use.

Requirements: IMAP PECL extension in PHP 8.4+; a TLS-protected mailbox.

Compatibility and security: As of PHP 8.4 the IMAP extension is installed separately from PECL.

These examples are for an isolated development environment. Hostnames, credentials and remote paths must be replaced with your approved configuration.

Example 1: Core Behavior


Read-only mailbox example where the API supports it.

<?php
$user = getenv('IMAP_USER'); $pass = getenv('IMAP_PASSWORD');
$server = getenv('IMAP_HOST');
if (!$user || !$pass || !$server) throw new RuntimeException('Missing IMAP settings');
$mailbox = '{' . $server . ':993/imap/ssl}INBOX';
$mb = imap_open($mailbox, $user, $pass);
if (!$mb) throw new RuntimeException("Mailbox unavailable");
// Ensure destination mailbox Processed exists before moving messages.
$value = imap_mail_move($mb, "1", "Processed");
echo $value === false ? "Unavailable" : json_encode($value, JSON_PARTIAL_OUTPUT_ON_ERROR);
imap_close($mb);

Observe the returned value or changed resource state. A successful function call can still require additional checks before a higher-level operation is considered successful.

Example 2: Alternative or Failure Case


Check the outcome against the active mailbox and its message sequence numbers.

<?php
$user = getenv('IMAP_USER'); $pass = getenv('IMAP_PASSWORD');
$server = getenv('IMAP_HOST');
if (!$user || !$pass || !$server) throw new RuntimeException('Missing IMAP settings');
$mailbox = '{' . $server . ':993/imap/ssl}Support';
$mb = imap_open($mailbox, $user, $pass);
if (!$mb) throw new RuntimeException("Mailbox unavailable");
// Ensure destination mailbox Processed exists before moving messages.
$value = imap_mail_move($mb, "1", "Processed");
echo $value === false ? "Unavailable" : json_encode($value, JSON_PARTIAL_OUTPUT_ON_ERROR);
imap_close($mb);

Compare the different inputs, environment assumptions or result checks. Do not use a transport-level success value as proof that all business-level requirements were met.

Practical Example: Applying the Rule


For live mailboxes use stable message UIDs and review any destructive action first.

<?php
$user = getenv('IMAP_USER'); $pass = getenv('IMAP_PASSWORD');
$server = getenv('IMAP_HOST');
if (!$user || !$pass || !$server) throw new RuntimeException('Missing IMAP settings');
$mailbox = '{' . $server . ':993/imap/ssl}Archive';
$mb = imap_open($mailbox, $user, $pass);
if (!$mb) throw new RuntimeException("Mailbox unavailable");
// Ensure destination mailbox Processed exists before moving messages.
$value = imap_mail_move($mb, "1", "Processed");
echo $value === false ? "Unavailable" : json_encode($value, JSON_PARTIAL_OUTPUT_ON_ERROR);
imap_close($mb);

In an application, convert low-level failures into controlled responses and log only non-sensitive diagnostic context.

Important Behavior and Edge Cases


Many servers implement movement as copy plus delete; check target mailbox and expunge behavior.

IMAP message sequence numbers can change as mailbox contents change; use UID-based options when stable identifiers matter.

Comparison with Related Functions or Patterns


ApproachWhen it differs
imap_mail_moveMoves message(s) to another IMAP mailbox.
Related API or patternimap_delete marks messages for deletion without moving them.

Common Beginner Mistakes


MistakeCorrect approach
Assuming a deleted or moved message can always be recoveredMany servers implement movement as copy plus delete; check target mailbox and expunge behavior.
Substituting a related API without checking its semanticsimap_delete marks messages for deletion without moving them.

Key Takeaways


  • Moves message(s) to another IMAP mailbox.
  • Many servers implement movement as copy plus delete; check target mailbox and expunge behavior.
  • imap_delete marks messages for deletion without moving them.

Select this API or pattern based on its documented behavior and the trust boundaries in your own application, and test all external integrations against their actual deployments.

Share this resourceSend this page to someone who may find it useful.