Skip to content

Commit

Permalink
RFC4315 MOVE fallback added #123 (thanks @freescout-help-desk)
Browse files Browse the repository at this point in the history
  • Loading branch information
Webklex committed Jan 17, 2025
1 parent cafda4f commit 512f9f5
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 2 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ Updates should follow the [Keep a CHANGELOG](http://keepachangelog.com/) princip
### Added
- Security configuration options added
- Spoofing detection added #40
- RFC4315 MOVE fallback added #123 (thanks @freescout-help-desk)

### Breaking changes
- NaN
Expand Down
42 changes: 40 additions & 2 deletions src/Connection/Protocols/ImapProtocol.php
Original file line number Diff line number Diff line change
Expand Up @@ -1141,7 +1141,25 @@ public function moveMessage(string $folder, $from, ?int $to = null, int|string $
$set = $this->buildSet($from, $to);
$command = $this->buildUIDCommand("MOVE", $uid);

return $this->requestAndResponse($command, [$set, $this->escapeString($folder)], true);
$result = $this->requestAndResponse($command, [$set, $this->escapeString($folder)], true);
// RFC4315 fallback to COPY, STORE and EXPUNGE.
// Required for cases where MOVE isn't supported by the server. So we copy the message to the target folder,
// mark the original message as deleted and expunge the mailbox.
// See the following links for more information:
// - https://github.com/freescout-help-desk/freescout/issues/4313
// - https://github.com/Webklex/php-imap/issues/123
if (!$result->boolean()) {
$result = $this->copyMessage($folder, $from, $to, $uid);
if (!$result->boolean()) {
return $result;
}
$result = $this->store(['\Deleted'], $from, $to, null, true, $uid);
if (!$result->boolean()) {
return $result;
}
return $this->expunge();
}
return $result;
}

/**
Expand All @@ -1163,7 +1181,27 @@ public function moveManyMessages(array $messages, string $folder, int|string $ui
$set = implode(',', $messages);
$tokens = [$set, $this->escapeString($folder)];

return $this->requestAndResponse($command, $tokens, true);
$result = $this->requestAndResponse($command, $tokens, true);
// RFC4315 fallback to COPY, STORE and EXPUNGE.
// Required for cases where MOVE isn't supported by the server. So we copy the message to the target folder,
// mark the original message as deleted and expunge the mailbox.
// See the following links for more information:
// - https://github.com/freescout-help-desk/freescout/issues/4313
// - https://github.com/Webklex/php-imap/issues/123
if (!$result->boolean()) {
$result = $this->copyManyMessages($messages, $folder, $uid);
if (!$result->boolean()) {
return $result;
}
foreach ($messages as $message) {
$result = $this->store(['\Deleted'], $message, $message, null, true, $uid);
if (!$result->boolean()) {
return $result;
}
}
return $this->expunge();
}
return $result;
}

/**
Expand Down

0 comments on commit 512f9f5

Please sign in to comment.