@@ -186,38 +186,40 @@ public function moveMessages(Account $account, Mailbox $targetMailbox, Mailbox $
186186 $ client = $ this ->protocolFactory ->imapClient ($ account );
187187
188188 $ mutatedMessages = [];
189- foreach ($ messages as $ message ) {
190- try {
191- $ newUid = $ this ->imapMessageMapper ->move ($ client , $ sourceMailbox ->getName (), $ message ->getUid (), $ targetMailbox ->getName ());
192- if ($ newUid === null ) {
193- // The IMAP server does not support UIDPLUS and the message has no Message-ID
194- // header, so the new UID is unknown. It will be reconciled on the next sync.
195- $ this ->logger ->debug ('Moved message but could not determine its new UID ' , [
189+ try {
190+ foreach ($ messages as $ message ) {
191+ try {
192+ $ newUid = $ this ->imapMessageMapper ->move ($ client , $ sourceMailbox ->getName (), $ message ->getUid (), $ targetMailbox ->getName ());
193+ if ($ newUid === null ) {
194+ // The IMAP server does not support UIDPLUS and the message has no Message-ID
195+ // header, so the new UID is unknown. It will be reconciled on the next sync.
196+ $ this ->logger ->debug ('Moved message but could not determine its new UID ' , [
197+ 'userId ' => $ account ->getUserId (),
198+ 'accountId ' => $ account ->getId (),
199+ 'sourceMailboxId ' => $ sourceMailbox ->getId (),
200+ 'targetMailboxId ' => $ targetMailbox ->getId (),
201+ 'messageUid ' => $ message ->getUid (),
202+ ]);
203+ continue ;
204+ }
205+ $ message ->setUid ($ newUid );
206+ $ message ->setMailboxId ($ targetMailbox ->getId ());
207+ $ mutatedMessages [] = $ message ;
208+ } catch (Horde_Imap_Client_Exception $ e ) {
209+ $ this ->logger ->error ('Could not move message on remote IMAP server ' , [
210+ 'exception ' => $ e ,
196211 'userId ' => $ account ->getUserId (),
197212 'accountId ' => $ account ->getId (),
198213 'sourceMailboxId ' => $ sourceMailbox ->getId (),
199214 'targetMailboxId ' => $ targetMailbox ->getId (),
200215 'messageUid ' => $ message ->getUid (),
201216 ]);
202- continue ;
203217 }
204- $ message ->setUid ($ newUid );
205- $ message ->setMailboxId ($ targetMailbox ->getId ());
206- $ mutatedMessages [] = $ message ;
207- } catch (Horde_Imap_Client_Exception $ e ) {
208- $ this ->logger ->error ('Could not move message on remote IMAP server ' , [
209- 'exception ' => $ e ,
210- 'userId ' => $ account ->getUserId (),
211- 'accountId ' => $ account ->getId (),
212- 'sourceMailboxId ' => $ sourceMailbox ->getId (),
213- 'targetMailboxId ' => $ targetMailbox ->getId (),
214- 'messageUid ' => $ message ->getUid (),
215- ]);
216218 }
219+ } finally {
220+ $ client ->logout ();
217221 }
218222
219- $ client ->logout ();
220-
221223 return $ mutatedMessages ;
222224 }
223225
@@ -229,23 +231,25 @@ public function deleteMessages(Account $account, Mailbox $mailbox, Message ...$m
229231 $ client = $ this ->protocolFactory ->imapClient ($ account );
230232
231233 $ mutatedMessages = [];
232- foreach ($ messages as $ message ) {
233- try {
234- $ this ->imapMessageMapper ->expunge ($ client , $ mailbox ->getName (), $ message ->getUid ());
235- $ mutatedMessages [] = $ message ;
236- } catch (Horde_Imap_Client_Exception $ e ) {
237- $ this ->logger ->error ('Could not delete message on remote IMAP server ' , [
238- 'exception ' => $ e ,
239- 'userId ' => $ account ->getUserId (),
240- 'accountId ' => $ account ->getId (),
241- 'mailboxId ' => $ mailbox ->getId (),
242- 'messageUid ' => $ message ->getUid (),
243- ]);
234+ try {
235+ foreach ($ messages as $ message ) {
236+ try {
237+ $ this ->imapMessageMapper ->expunge ($ client , $ mailbox ->getName (), $ message ->getUid ());
238+ $ mutatedMessages [] = $ message ;
239+ } catch (Horde_Imap_Client_Exception $ e ) {
240+ $ this ->logger ->error ('Could not delete message on remote IMAP server ' , [
241+ 'exception ' => $ e ,
242+ 'userId ' => $ account ->getUserId (),
243+ 'accountId ' => $ account ->getId (),
244+ 'mailboxId ' => $ mailbox ->getId (),
245+ 'messageUid ' => $ message ->getUid (),
246+ ]);
247+ }
244248 }
249+ } finally {
250+ $ client ->logout ();
245251 }
246252
247- $ client ->logout ();
248-
249253 return $ mutatedMessages ;
250254 }
251255
@@ -276,10 +280,10 @@ public function flagMessages(Account $account, Mailbox $mailbox, string $flag, b
276280 }
277281 } catch (Horde_Imap_Client_Exception $ e ) {
278282 throw new ServiceException ('Could not set message flag on remote IMAP server: ' . $ e ->getMessage (), $ e ->getCode (), $ e );
283+ } finally {
284+ $ client ->logout ();
279285 }
280286
281- $ client ->logout ();
282-
283287 return $ messages ;
284288 }
285289
@@ -290,20 +294,24 @@ public function tagMessages(Account $account, Mailbox $mailbox, Tag $tag, bool $
290294 }
291295 $ client = $ this ->protocolFactory ->imapClient ($ account );
292296
293- if ($ this ->isPermflagsEnabledWithClient ($ client , $ mailbox ->getName ()) === false ) {
294- $ this ->logger ->error ('Cannot set message keyword, server does not support permanent flags ' , ['tag ' => $ tag ->getDisplayName ()]);
295- return [];
296- }
297-
298- $ uids = array_map (static fn (Message $ message ) => $ message ->getUid (), $ messages );
299297 try {
300- if ($ value ) {
301- $ this ->imapMessageMapper ->addFlag ($ client , $ mailbox , $ uids , $ tag ->getImapLabel ());
302- } else {
303- $ this ->imapMessageMapper ->removeFlag ($ client , $ mailbox , $ uids , $ tag ->getImapLabel ());
298+ if ($ this ->isPermflagsEnabledWithClient ($ client , $ mailbox ->getName ()) === false ) {
299+ $ this ->logger ->error ('Cannot set message keyword, server does not support permanent flags ' , ['tag ' => $ tag ->getDisplayName ()]);
300+ return [];
304301 }
305- } catch (Horde_Imap_Client_Exception $ e ) {
306- throw new ServiceException ('Could not set message keyword on remote IMAP server: ' . $ e ->getMessage (), $ e ->getCode (), $ e );
302+
303+ $ uids = array_map (static fn (Message $ message ) => $ message ->getUid (), $ messages );
304+ try {
305+ if ($ value ) {
306+ $ this ->imapMessageMapper ->addFlag ($ client , $ mailbox , $ uids , $ tag ->getImapLabel ());
307+ } else {
308+ $ this ->imapMessageMapper ->removeFlag ($ client , $ mailbox , $ uids , $ tag ->getImapLabel ());
309+ }
310+ } catch (Horde_Imap_Client_Exception $ e ) {
311+ throw new ServiceException ('Could not set message keyword on remote IMAP server: ' . $ e ->getMessage (), $ e ->getCode (), $ e );
312+ }
313+ } finally {
314+ $ client ->logout ();
307315 }
308316
309317 foreach ($ messages as $ message ) {
@@ -359,7 +367,11 @@ public function repairSync(Account $account, Mailbox $mailbox): void {
359367 #[\Override]
360368 public function isPermflagsEnabled (Account $ account , Mailbox $ mailbox ): bool {
361369 $ client = $ this ->protocolFactory ->imapClient ($ account );
362- return $ this ->isPermflagsEnabledWithClient ($ client , $ mailbox ->getName ());
370+ try {
371+ return $ this ->isPermflagsEnabledWithClient ($ client , $ mailbox ->getName ());
372+ } finally {
373+ $ client ->logout ();
374+ }
363375 }
364376
365377 private function isPermflagsEnabledWithClient ($ client , string $ mailbox ): bool {
0 commit comments