diff --git a/src/app/Backends/IMAP.php b/src/app/Backends/IMAP.php --- a/src/app/Backends/IMAP.php +++ b/src/app/Backends/IMAP.php @@ -420,6 +420,55 @@ return true; } + /** + * Rename a folder. + * + * @param string $sourceMailbox Source Mailbox + * @param string $targetMailbox Target Mailbox + * + * @return bool True if the mailbox was renamed successfully, False otherwise + * @throws \Exception + */ + public static function renameMailbox($sourceMailbox, $targetMailbox): bool + { + $config = self::getConfig(); + $imap = self::initIMAP($config); + + $sourceMailbox = self::toUTF7($sourceMailbox); + $targetMailbox = self::toUTF7($targetMailbox); + + // Rename the mailbox (only possible if we have the old folder) + if (!empty($targetMailbox) && $targetMailbox != $sourceMailbox) { + if (!$imap->renameFolder($sourceMailbox, $targetMailbox)) { + \Log::error("Failed to rename mailbox {$sourceMailbox} to {$targetMailbox}"); + $imap->closeConnection(); + return false; + } + } + + $imap->closeConnection(); + + return true; + } + + public static function userMailbox(string $user, string $mailbox): string + { + [$localpart, $domain] = explode('@', $user, 2); + return "user/{$localpart}/{$mailbox}@{$domain}"; + } + + public static function listMailboxes(string $user): array + { + $config = self::getConfig(); + $imap = self::initIMAP($config); + + $result = $imap->listMailboxes('', self::userMailbox($user, "*")); + + $imap->closeConnection(); + + return $result; + } + /** * Convert UTF8 string to UTF7-IMAP encoding */ diff --git a/src/app/Console/Commands/Imap/ListCommand.php b/src/app/Console/Commands/Imap/ListCommand.php new file mode 100644 --- /dev/null +++ b/src/app/Console/Commands/Imap/ListCommand.php @@ -0,0 +1,35 @@ +argument('user'); + foreach (\App\Backends\IMAP::listMailboxes($user) as $mailbox) { + $this->info("$mailbox"); + } + } +} diff --git a/src/app/Console/Commands/Imap/RenameCommand.php b/src/app/Console/Commands/Imap/RenameCommand.php new file mode 100644 --- /dev/null +++ b/src/app/Console/Commands/Imap/RenameCommand.php @@ -0,0 +1,39 @@ +argument('user'); + $sourceMailbox = $this->argument('sourceMailbox'); + $targetMailbox = $this->argument('targetMailbox'); + + \App\Backends\IMAP::renameMailbox( + self::userMailbox($user, $sourceMailbox), + self::userMailbox($user, $targetMailbox) + ); + } +}