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)