Skip to content

Conversation

@pixelpeter
Copy link
Contributor

Currently it's not possible to search multiple folders without re-connecting to the server.
This PR can solve this issue
Just added this simple function to Mailbox.php

    public function switchMailbox($imapPath = '') {
        $this->imapPath = $imapPath;
        $imapStream = @imap_reopen($this->getImapStream(), $imapPath);
        if(!$imapStream) {
            throw new Exception("Couldn't switch  mailbox: " . imap_last_error());
        }
    }

Now you can do this:

// Connect to INBOX on server and search for mails
$mailbox = new PhpImap\Mailbox('{imap.example.org.com:993/imap/ssl}INBOX', '[email protected]', '*********', __DIR__);
$mailIds = $mailbox->searchMailbox('ALL');

// store all mails in $inbox
$inbox = [];
foreach($mailIds as $mailId)
{
    $inbox[] = $mailbox->getMail($mailId, false);
}

// Switch to other folder without opening new connection
$mailbox->switchMailbox('{imap.example.org.com:993/imap/ssl}INBOX.Sent');
$mailIds = $mailbox->searchMailbox('ALL');

// store all mails in $sent
$sent = [];
foreach($mailIds as $mailId)
{
    $sent[] = $mailbox->getMail($mailId, false);
}

HINT: It's important to fetch the actual mails BEFORE switching to another mailbox.

Hope you'll find this useful.

@melino
Copy link

melino commented Jun 5, 2016

Fantastic, exactly what I have just missed. Thx!

@underdpt
Copy link
Contributor

underdpt commented Jul 14, 2016

Shouldn't this patch set the class variable $imapStream? At the end of the function:

$this->imapStream = $imapStream;

@barbushin barbushin merged commit e2e8e23 into barbushin:master Aug 13, 2016
@decadence
Copy link
Contributor

#187

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants