- sqimap_mailbox_create($imap_stream, $trash_folder . $delimiter . $subFolderName, "");
- $mbx_response = sqimap_mailbox_select($imap_stream, $tree[$index]['value']);
- $messageCount = $mbx_response['EXISTS'];
- if ($messageCount > 0)
- sqimap_messages_copy($imap_stream, 1, '*', $trash_folder . $delimiter . $subFolderName);
+ // create new trash subfolder only if it does not exist.
+ if (!sqimap_mailbox_exists($imap_stream, $trash_folder . $delimiter . $subFolderName))
+ sqimap_mailbox_create($imap_stream, $trash_folder . $delimiter . $subFolderName, "");
+
+ $mbx_response = sqimap_mailbox_select($imap_stream, $tree[$index]['value']);
+ $messageCount = $mbx_response['EXISTS'];
+ if ($messageCount > 0) {
+ sqimap_msgs_list_copy($imap_stream, '1:*', $trash_folder . $delimiter . $subFolderName);
+ }
+ // after copy close the mailbox to get in unselected state
+ sqimap_run_command($imap_stream,'CLOSE',false,$response,$message);