Skip to content
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 19 additions & 25 deletions lib/Send/CopySentMessageHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,23 @@
*/
namespace OCA\Mail\Send;

use Horde_Imap_Client;
use Horde_Imap_Client_Exception;
use Horde_Imap_Client_Socket;
use OCA\Mail\Account;
use OCA\Mail\Db\LocalMessage;
use OCA\Mail\Db\Mailbox;
use OCA\Mail\Db\MailboxMapper;
use OCA\Mail\IMAP\MessageMapper;
use OCP\AppFramework\Db\DoesNotExistException;
use OCA\Mail\Service\MailManager;
use Psr\Log\LoggerInterface;

class CopySentMessageHandler extends AHandler {
public function __construct(
private MailboxMapper $mailboxMapper,
private LoggerInterface $logger,
private MessageMapper $messageMapper,
private MailManager $mailManager,
) {
}

Expand All @@ -40,30 +43,7 @@ public function process(
return $localMessage;
}

$sentMailboxId = $account->getMailAccount()->getSentMailboxId();
if ($sentMailboxId === null) {
// We can't write the "sent mailbox" status here bc that would trigger an additional send.
// Thus, we leave the "imap copy to sent mailbox" status.
$localMessage->setStatus(LocalMessage::STATUS_IMAP_SENT_MAILBOX_FAIL);
$this->logger->warning("No sent mailbox exists, can't save sent message");
return $localMessage;
}

// Save the message in the sent mailbox
try {
$sentMailbox = $this->mailboxMapper->findById(
$sentMailboxId
);
} catch (DoesNotExistException $e) {
// We can't write the "sent mailbox" status here bc that would trigger an additional send.
// Thus, we leave the "imap copy to sent mailbox" status.
$localMessage->setStatus(LocalMessage::STATUS_IMAP_SENT_MAILBOX_FAIL);
$this->logger->error('Sent mailbox could not be found', [
'exception' => $e,
]);

return $localMessage;
}
$sentMailbox = $this->findOrCreateSentMailbox($account);

try {
$this->messageMapper->save(
Expand All @@ -82,4 +62,18 @@ public function process(

return $this->processNext($account, $localMessage, $client);
}

private function findOrCreateSentMailbox(Account $account): Mailbox {
$sentMailboxId = $account->getMailAccount()->getSentMailboxId();

if ($sentMailboxId === null) {
return $this->mailManager->createMailbox(
$account,
'Sents',
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
'Sents',
'Sent',

Copy link
Contributor Author

@hamza221 hamza221 Mar 12, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Put that in for testing , I'll change it after testing

[Horde_Imap_Client::SPECIALUSE_SENT]
);
}

return $this->mailboxMapper->findById($sentMailboxId);
}
}
Loading