Skip to content

Commit 5c7d07a

Browse files
committed
fix: no sent mailbox
Signed-off-by: Hamza <hamzamahjoubi221@gmail.com>
1 parent 329472a commit 5c7d07a

File tree

1 file changed

+19
-25
lines changed

1 file changed

+19
-25
lines changed

lib/Send/CopySentMessageHandler.php

Lines changed: 19 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -7,20 +7,23 @@
77
*/
88
namespace OCA\Mail\Send;
99

10+
use Horde_Imap_Client;
1011
use Horde_Imap_Client_Exception;
1112
use Horde_Imap_Client_Socket;
1213
use OCA\Mail\Account;
1314
use OCA\Mail\Db\LocalMessage;
15+
use OCA\Mail\Db\Mailbox;
1416
use OCA\Mail\Db\MailboxMapper;
1517
use OCA\Mail\IMAP\MessageMapper;
16-
use OCP\AppFramework\Db\DoesNotExistException;
18+
use OCA\Mail\Service\MailManager;
1719
use Psr\Log\LoggerInterface;
1820

1921
class CopySentMessageHandler extends AHandler {
2022
public function __construct(
2123
private MailboxMapper $mailboxMapper,
2224
private LoggerInterface $logger,
2325
private MessageMapper $messageMapper,
26+
private MailManager $mailManager,
2427
) {
2528
}
2629

@@ -40,30 +43,7 @@ public function process(
4043
return $localMessage;
4144
}
4245

43-
$sentMailboxId = $account->getMailAccount()->getSentMailboxId();
44-
if ($sentMailboxId === null) {
45-
// We can't write the "sent mailbox" status here bc that would trigger an additional send.
46-
// Thus, we leave the "imap copy to sent mailbox" status.
47-
$localMessage->setStatus(LocalMessage::STATUS_IMAP_SENT_MAILBOX_FAIL);
48-
$this->logger->warning("No sent mailbox exists, can't save sent message");
49-
return $localMessage;
50-
}
51-
52-
// Save the message in the sent mailbox
53-
try {
54-
$sentMailbox = $this->mailboxMapper->findById(
55-
$sentMailboxId
56-
);
57-
} catch (DoesNotExistException $e) {
58-
// We can't write the "sent mailbox" status here bc that would trigger an additional send.
59-
// Thus, we leave the "imap copy to sent mailbox" status.
60-
$localMessage->setStatus(LocalMessage::STATUS_IMAP_SENT_MAILBOX_FAIL);
61-
$this->logger->error('Sent mailbox could not be found', [
62-
'exception' => $e,
63-
]);
64-
65-
return $localMessage;
66-
}
46+
$sentMailbox = $this->findOrCreateSentMailbox($account);
6747

6848
try {
6949
$this->messageMapper->save(
@@ -82,4 +62,18 @@ public function process(
8262

8363
return $this->processNext($account, $localMessage, $client);
8464
}
65+
66+
private function findOrCreateSentMailbox(Account $account): Mailbox {
67+
$sentMailboxId = $account->getMailAccount()->getSentMailboxId();
68+
69+
if ($sentMailboxId === null) {
70+
return $this->mailManager->createMailbox(
71+
$account,
72+
'Sents',
73+
[Horde_Imap_Client::SPECIALUSE_SENT]
74+
);
75+
}
76+
77+
return $this->mailboxMapper->findById($sentMailboxId);
78+
}
8579
}

0 commit comments

Comments
 (0)