Skip to content

Commit 512f9f5

Browse files
committed
RFC4315 MOVE fallback added Webklex#123 (thanks @freescout-help-desk)
1 parent cafda4f commit 512f9f5

File tree

2 files changed

+41
-2
lines changed

2 files changed

+41
-2
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ Updates should follow the [Keep a CHANGELOG](http://keepachangelog.com/) princip
1212
### Added
1313
- Security configuration options added
1414
- Spoofing detection added #40
15+
- RFC4315 MOVE fallback added #123 (thanks @freescout-help-desk)
1516

1617
### Breaking changes
1718
- NaN

src/Connection/Protocols/ImapProtocol.php

Lines changed: 40 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1141,7 +1141,25 @@ public function moveMessage(string $folder, $from, ?int $to = null, int|string $
11411141
$set = $this->buildSet($from, $to);
11421142
$command = $this->buildUIDCommand("MOVE", $uid);
11431143

1144-
return $this->requestAndResponse($command, [$set, $this->escapeString($folder)], true);
1144+
$result = $this->requestAndResponse($command, [$set, $this->escapeString($folder)], true);
1145+
// RFC4315 fallback to COPY, STORE and EXPUNGE.
1146+
// Required for cases where MOVE isn't supported by the server. So we copy the message to the target folder,
1147+
// mark the original message as deleted and expunge the mailbox.
1148+
// See the following links for more information:
1149+
// - https://github.com/freescout-help-desk/freescout/issues/4313
1150+
// - https://github.com/Webklex/php-imap/issues/123
1151+
if (!$result->boolean()) {
1152+
$result = $this->copyMessage($folder, $from, $to, $uid);
1153+
if (!$result->boolean()) {
1154+
return $result;
1155+
}
1156+
$result = $this->store(['\Deleted'], $from, $to, null, true, $uid);
1157+
if (!$result->boolean()) {
1158+
return $result;
1159+
}
1160+
return $this->expunge();
1161+
}
1162+
return $result;
11451163
}
11461164

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

1166-
return $this->requestAndResponse($command, $tokens, true);
1184+
$result = $this->requestAndResponse($command, $tokens, true);
1185+
// RFC4315 fallback to COPY, STORE and EXPUNGE.
1186+
// Required for cases where MOVE isn't supported by the server. So we copy the message to the target folder,
1187+
// mark the original message as deleted and expunge the mailbox.
1188+
// See the following links for more information:
1189+
// - https://github.com/freescout-help-desk/freescout/issues/4313
1190+
// - https://github.com/Webklex/php-imap/issues/123
1191+
if (!$result->boolean()) {
1192+
$result = $this->copyManyMessages($messages, $folder, $uid);
1193+
if (!$result->boolean()) {
1194+
return $result;
1195+
}
1196+
foreach ($messages as $message) {
1197+
$result = $this->store(['\Deleted'], $message, $message, null, true, $uid);
1198+
if (!$result->boolean()) {
1199+
return $result;
1200+
}
1201+
}
1202+
return $this->expunge();
1203+
}
1204+
return $result;
11671205
}
11681206

11691207
/**

0 commit comments

Comments
 (0)