-
Notifications
You must be signed in to change notification settings - Fork 328
Expand file tree
/
Copy pathCopySentMessageHandler.php
More file actions
116 lines (104 loc) · 3.74 KB
/
Copy pathCopySentMessageHandler.php
File metadata and controls
116 lines (104 loc) · 3.74 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
<?php
declare(strict_types=1);
/**
* SPDX-FileCopyrightText: 2024 Nextcloud GmbH and Nextcloud contributors
* SPDX-License-Identifier: AGPL-3.0-or-later
*/
namespace OCA\Mail\Send;
use Horde_Imap_Client_Exception;
use Horde_Imap_Client_Socket;
use OCA\Mail\Account;
use OCA\Mail\AppInfo\Application;
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 OCP\IConfig;
use Psr\Log\LoggerInterface;
class CopySentMessageHandler extends AHandler {
public function __construct(
private MailboxMapper $mailboxMapper,
private LoggerInterface $logger,
private MessageMapper $messageMapper,
private IConfig $config,
) {
}
private function isAlreadySavedByServer(
Horde_Imap_Client_Socket $client,
Mailbox $mailbox,
string $rawMessage,
): bool {
[$headers] = preg_split("/\r?\n\r?\n/", $rawMessage, 2);
if (!preg_match('/^Message-ID:\s*(<[^>\r\n]+>)/im', $headers, $m)) {
return false;
}
try {
return $this->messageMapper->existsInMailboxByMessageId($client, $mailbox, $m[1]);
} catch (Horde_Imap_Client_Exception $e) {
$this->logger->warning('Could not search for existing sent message, proceeding with APPEND', ['exception' => $e]);
return false;
}
}
#[\Override]
public function process(
Account $account,
LocalMessage $localMessage,
Horde_Imap_Client_Socket $client,
): LocalMessage {
if ($localMessage->getStatus() === LocalMessage::STATUS_PROCESSED) {
return $this->processNext($account, $localMessage, $client);
}
$rawMessage = $localMessage->getRaw();
if ($rawMessage === null) {
$localMessage->setStatus(LocalMessage::STATUS_IMAP_SENT_MAILBOX_FAIL);
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;
}
// Some servers (e.g. Exchange) auto-save sent messages, so skip the APPEND when the
// message is already present to avoid duplicates in the Sent folder.
// Opt-in via: occ config:app:set mail smtp_saves_sent --value=true
if ($this->config->getAppValue(Application::APP_ID, 'smtp_saves_sent', 'false') === 'true'
&& $this->isAlreadySavedByServer($client, $sentMailbox, $rawMessage)) {
$this->logger->debug('Sent message already present in sent mailbox (server auto-saved), skipping APPEND');
$localMessage->setStatus(LocalMessage::STATUS_PROCESSED);
return $this->processNext($account, $localMessage, $client);
}
try {
$this->messageMapper->save(
$client,
$sentMailbox,
$rawMessage,
);
$localMessage->setStatus(LocalMessage::STATUS_PROCESSED);
} catch (Horde_Imap_Client_Exception $e) {
$localMessage->setStatus(LocalMessage::STATUS_IMAP_SENT_MAILBOX_FAIL);
$this->logger->error('Could not copy message to sent mailbox', [
'exception' => $e,
]);
return $localMessage;
}
return $this->processNext($account, $localMessage, $client);
}
}