From 320db9f82be008b2284cd16a88425978fe65712c Mon Sep 17 00:00:00 2001 From: Sebastian Krupinski Date: Tue, 25 Aug 2026 22:42:16 -0400 Subject: [PATCH] fix: move draft to sent and clear draft flag on submission Signed-off-by: Sebastian Krupinski --- lib/Providers/Mail/Service.php | 19 +++++++++++++++++++ lib/Service/Remote/RemoteMailService.php | 12 ++++++++++-- 2 files changed, 29 insertions(+), 2 deletions(-) diff --git a/lib/Providers/Mail/Service.php b/lib/Providers/Mail/Service.php index e3db676..f2362b5 100644 --- a/lib/Providers/Mail/Service.php +++ b/lib/Providers/Mail/Service.php @@ -950,6 +950,23 @@ class Service implements ServiceBaseInterface, ServiceMutableInterface, ServiceC ); } + // determine the sent folder + // if no explicit sent folder is configured, we will attempt to find a folder with the "sent" role. + $postSentTarget = $this->auxiliary['sentTarget'] ?? null; + $postSentTarget = $postSentTarget !== null && (string) $postSentTarget !== '' + ? new CollectionIdentifier($this->provider(), (string) $this->identifier(), (string) $postSentTarget) + : $this->resolveCollectionByRole(CollectionRoles::Sent->value); + + if ($postSentTarget === null + || $postSentTarget->provider() !== $this->provider() + || $postSentTarget->service() !== (string)$this->identifier()) { + return new EntitySubmitResult( + EntitySubmitResult::DISPOSITION_ERROR, + errorCode: 'sent_collection_not_found', + errorMessage: 'No Sent target collection is configured or could be resolved for this service', + ); + } + if ($message !== null) { [$nativeMessage, $nativeAttachments] = $this->messagePayload($message); } else { @@ -960,6 +977,8 @@ class Service implements ServiceBaseInterface, ServiceMutableInterface, ServiceC $transportId = $this->mailService->entitySubmitDraft( $identityId, (string)$source->entity(), + (string)$source->collection(), + $postSentTarget->collection(), $nativeMessage, $nativeAttachments, ); diff --git a/lib/Service/Remote/RemoteMailService.php b/lib/Service/Remote/RemoteMailService.php index 73610a0..d679d99 100644 --- a/lib/Service/Remote/RemoteMailService.php +++ b/lib/Service/Remote/RemoteMailService.php @@ -1007,11 +1007,14 @@ class RemoteMailService { } /** - * Submit an existing draft message. + * Submit an existing draft message. On success, the draft Email is moved + * from the drafts mailbox to the sent mailbox and its \Draft keyword is + * cleared in the same request, so the submitted draft becomes the sent + * copy rather than lingering as an unsent draft. * * @since Release 2.0.0 */ - public function entitySubmitDraft(string $identityId, string $draftId, ?array $patch = null, array $attachments = []): string { + public function entitySubmitDraft(string $identityId, string $draftId, string $preSendTarget, string $postSentTarget, ?array $patch = null, array $attachments = []): string { if ($draftId === '') { throw new Exception('Draft identifier is required for draft submission', 1); } @@ -1029,6 +1032,11 @@ class RemoteMailService { $submissionId = 's_' . uniqid(); $r1 = new MailSubmissionSet($this->dataAccount, null, $this->resourceNamespace, $this->resourceEntityLabel); + $r1->completionUpdate($draftId, [ + 'mailboxIds/' . $postSentTarget => true, + 'mailboxIds/' . $preSendTarget => null, + 'keywords/$draft' => null, + ]); $s0 = $r1->create($submissionId); $s0->identity($identityId); $s0->message($draftId);