Skip to content

move, delete and get new #587

Description

@nico-martin

I have a function that should move an Email to a new folder, deletes the email in the current folder and then returns the new Email:

function emailMove(MessageInterface $mail, string $newFolder): MessageInterface
{
  $mailId = $mail->getId();
  $mailbox = $this->connection->getMailbox($newFolder);
  $mail->move($mailbox);
  $mail->delete();
  $this->connection->expunge();
  $messages = $mailbox->getMessages(
    new RawExpression(
      sprintf('TEXT "Message-ID: %s"', $mailId)
    )
  );
  $newMessageId = $messages[0] ?? null;
  if (!$newMessageId) {
    throw new Error('error', 'Email not moved');
  }
  $this->folder = $newFolder;
  return $mailbox->getMessage($newMessageId);
}

The problem is that the way to search the email by Message-ID does not seem to be very reliable. For example it does not work if the änewFolder is the trash.
Is there a better way?

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions