0

I'm trying to create simple integration with imap mail account but I am not able to open Gmail default mailbox: Ważne.

Gmail returns mailboxes as follows:

array:9 [▼
  0 => "{imap.gmail.com:993/imap/ssl}INBOX"
  1 => "{imap.gmail.com:993/imap/ssl}Sent"
  2 => "{imap.gmail.com:993/imap/ssl}[Gmail]/Kosz"
  3 => "{imap.gmail.com:993/imap/ssl}[Gmail]/Oznaczone gwiazdk\x01\x05"
  4 => "{imap.gmail.com:993/imap/ssl}[Gmail]/Spam"
  5 => "{imap.gmail.com:993/imap/ssl}[Gmail]/Wa\x01|ne"
  6 => "{imap.gmail.com:993/imap/ssl}[Gmail]/Wersje robocze"
  7 => "{imap.gmail.com:993/imap/ssl}[Gmail]/Wszystkie"
  8 => "{imap.gmail.com:993/imap/ssl}[Gmail]/Wys\x01Bane"
]

php-imap package I am using gives me an ability to switch between mailboxes using switchMailbox method.

Here is sample of code from that package:

public function switchMailbox($imapPath) {
    $this->imapPath = $imapPath;
    $this->imap('reopen', $this->imapPath);
}

public function imap($methodShortName, $args = [], $prependConnectionAsFirstArg = true, $throwExceptionClass = Exception::class) {
    if(!is_array($args)) {
        $args = [$args];
    }
    foreach($args as &$arg) {
        if(is_string($arg)) {
            $arg = imap_utf7_encode($arg);
        }
    }
    if($prependConnectionAsFirstArg) {
        array_unshift($args, $this->getImapStream());
    }

    imap_errors(); // flush errors
    $result = @call_user_func_array("imap_$methodShortName", $args);

    $errors = imap_errors();

    if($errors) {
        if($throwExceptionClass) {
            throw new $throwExceptionClass("IMAP method imap_$methodShortName() failed with error: " . implode('. ', $errors));
        }
        else {
            return false;
        }
    }

    return $result;
}

The problem occurs when I am trying to fetch. Sometimes I am getting this error:

 IMAP protocol error: Could not parse command. Could not parse command

but sometimes error says tells that happened cause of unknown mailbox...

Here is sample of my code:

// getting imap object - built with user credentials
$mailbox = auth()->user()->mailbox->getIMAP(); 

$folders = $mailbox->getListingFolders();

// "ważne" folder. remember that "ż" was returned as string provided above
$mailbox->switchMailbox($folders[5]); 

// and boom! running this line breaks everything. Kids are crying, womens are screaming....
$mailbox->statusMailbox()

How can I solve it?


UPDATE

I tried to directly pass mailbox name - not from returned array. Sample code changed as follows:

$mailbox->switchMailbox('{imap.gmail.com:993/imap/ssl}[Gmail]/Ważne');

but the error changed too:

IMAP method imap_reopen() failed with error: [NONEXISTENT] Unknown Mailbox: [Gmail]/Wa&xbw-ne (now in authenticated state) (Failure)
piotr
  • 1,282
  • 1
  • 8
  • 20
  • Where's the code where you call these functions? – Mike Oct 19 '17 at 20:58
  • What line is producing the error? – Mike Oct 19 '17 at 21:06
  • `$mailbox->statusMailbox()` cannot parse command(this error ocurs today, yesterday the error was caused by "unknown mailbox". I cant figure out how should I encode this mailbox name. – piotr Oct 19 '17 at 21:09
  • I have no idea what's going on, but mailbox names shouldn't have non-ascii characters in them (they are encoded), so those \x01s are extremely suspicious. Either a library problem or a gmail problem. Your example should look like 'Wa&AXw-ne" or similar. – Max Oct 19 '17 at 21:09
  • look at my update – piotr Oct 19 '17 at 21:19
  • What does `var_dump($folders[5])` output? – Mike Oct 19 '17 at 21:22
  • You are really overdoing the UTF-7 encoding. It ONLY applies to mailbox names. It also appears to be the wrong character. AXw != xbw – Max Oct 19 '17 at 21:43
  • That is, whatever you're using to download the mailbox names is wrong, or you've got the wrong encoding set on whatever you used to view it. What is your code for listing those names? – Max Oct 20 '17 at 15:14

0 Answers0