From 9558456bcf4e4c4accdb6e3fb6706f57ca651f0f Mon Sep 17 00:00:00 2001 From: the-djmaze <> Date: Mon, 22 Jan 2024 11:07:07 +0100 Subject: [PATCH] Changes for #1397 --- .../libraries/RainLoop/Actions/Messages.php | 88 +++++++++++++------ 1 file changed, 60 insertions(+), 28 deletions(-) diff --git a/snappymail/v/0.0.0/app/libraries/RainLoop/Actions/Messages.php b/snappymail/v/0.0.0/app/libraries/RainLoop/Actions/Messages.php index 3053c24b7..8ce3917d7 100644 --- a/snappymail/v/0.0.0/app/libraries/RainLoop/Actions/Messages.php +++ b/snappymail/v/0.0.0/app/libraries/RainLoop/Actions/Messages.php @@ -156,7 +156,7 @@ trait Messages $oConfig = $this->Config(); - $sSentFolder = $this->GetActionParam('saveFolder', ''); + $sSaveFolder = $this->GetActionParam('saveFolder', ''); $aDraftInfo = $this->GetActionParam('draftInfo', null); $oMessage = $this->buildMessage($oAccount, false); @@ -174,9 +174,10 @@ trait Messages ); if (false !== $iMessageStreamSize) { - $bDsn = !empty($this->GetActionParam('dsn', 0)); - $bRequireTLS = !empty($this->GetActionParam('requireTLS', 0)); - $this->smtpSendMessage($oAccount, $oMessage, $rMessageStream, $iMessageStreamSize, true, $bDsn, $bRequireTLS); + $this->smtpSendMessage($oAccount, $oMessage, $rMessageStream, $iMessageStreamSize, true, + !empty($this->GetActionParam('dsn', 0)), + !empty($this->GetActionParam('requireTLS', 0)) + ); if (\is_array($aDraftInfo) && 3 === \count($aDraftInfo)) { $sDraftInfoType = $aDraftInfo[0]; @@ -202,43 +203,74 @@ trait Messages } } - if (\strlen($sSentFolder)) { + if (\strlen($sSaveFolder)) { + $rAppendMessageStream = $rMessageStream; + $iAppendMessageStreamSize = $iMessageStreamSize; try { - if (!$oMessage->GetBcc()) { - if (\is_resource($rMessageStream)) { - \rewind($rMessageStream); - } - - $this->Plugins()->RunHook('filter.send-message-stream', - array($oAccount, &$rMessageStream, &$iMessageStreamSize)); - - $this->ImapClient()->MessageAppendStream( - $sSentFolder, $rMessageStream, $iMessageStreamSize, array(MessageFlag::SEEN) - ); - } else { + if ($oMessage->GetBcc()) { $rAppendMessageStream = \MailSo\Base\ResourceRegistry::CreateMemoryResource(); - $iAppendMessageStreamSize = \MailSo\Base\Utils::WriteStream( $oMessage->ToStream(false), $rAppendMessageStream, 8192, true, true ); - - $this->Plugins()->RunHook('filter.send-message-stream', - array($oAccount, &$rAppendMessageStream, &$iAppendMessageStreamSize)); - - $this->ImapClient()->MessageAppendStream( - $sSentFolder, $rAppendMessageStream, $iAppendMessageStreamSize, array(MessageFlag::SEEN) - ); - - if (\is_resource($rAppendMessageStream)) { - fclose($rAppendMessageStream); + } else { + if (\is_resource($rMessageStream)) { + \rewind($rMessageStream); } } } catch (\Throwable $oException) { + $this->logException($oException, \LOG_ERR); throw new ClientException(Notifications::CantSaveMessage, $oException); } + + try + { + $this->Plugins()->RunHook('filter.send-message-stream', + array($oAccount, &$rAppendMessageStream, &$iAppendMessageStreamSize)); + } + catch (\Throwable $oException) + { + $this->logException($oException, \LOG_ERR); + } + + try + { + $this->ImapClient()->MessageAppendStream( + $sSaveFolder, $rAppendMessageStream, $iAppendMessageStreamSize, + array(MessageFlag::SEEN) + ); + } + catch (\Throwable $oException) + { + // Save folder not the same as default Sent folder, so try again + $oSettingsLocal = $this->SettingsProvider(true)->Load($oAccount); + if ($oSettingsLocal instanceof \RainLoop\Settings) { + $sSentFolder = (string) $oSettingsLocal->GetConf('SentFolder', ''); + if (\strlen($sSentFolder) && $sSentFolder !== $sSaveFolder) { + $oException = null; + try + { + $this->ImapClient()->MessageAppendStream( + $sSentFolder, $rAppendMessageStream, $iAppendMessageStreamSize, + array(MessageFlag::SEEN) + ); + } + catch (\Throwable $oException) + { + } + } + } + if ($oException) { + $this->logException($oException, \LOG_ERR); + throw new ClientException(Notifications::CantSaveMessage, $oException); + } + } + + if (\is_resource($rAppendMessageStream) && $rAppendMessageStream !== $rMessageStream) { + \fclose($rAppendMessageStream); + } } if (\is_resource($rMessageStream)) {