php-imap: Problem with serverEncoding

Hey,

I get the following error, when using the server encoding:

PHP Fatal error:  Uncaught exception 'ErrorException' with message 'Unknown: [BADCHARSET (US-ASCII)] The specified charset is not supported. (errflg=2)' in Unknown:0
Stack trace:
#0 [internal function]: Illuminate\Foundation\Bootstrap\HandleExceptions->handleError(8, 'Unknown: [BADCH...', 'Unknown', 0, Array)
#1 {main}
  thrown in Unknown on line 0

If I remove the server encoding from imap_search, everything works fine. With it I dont get any results and this error. Does anyone have an idea what the problem is (I’m trying to read from an exchange mailbox)?

About this issue

  • Original URL
  • State: closed
  • Created 9 years ago
  • Comments: 18 (1 by maintainers)

Commits related to this issue

Most upvoted comments

PHP Fatal error: Uncaught ErrorException: Unknown: [BADCHARSET (US-ASCII)]

Before:

        $mailsIds = $mailbox->searchMailbox('NEW');

After:

        try {
            retry:
            $mailsIds = $mailbox->searchMailbox('NEW');
        } catch (Exception $e) {
            // echo $e;
            if (strpos($e, " [BADCHARSET (US-ASCII)]")) {
                $oldEncoding = $mailbox->getServerEncoding();
                $mailbox->setServerEncoding('US-ASCII');
                $newEncoding = $mailbox->getServerEncoding();
                echo "Encoding changed dynamically from {$oldEncoding} to {$newEncoding}.\n";
                goto retry;
            } else {
                throw $e;
            }
        }

Summary: Works perfectly without diggin’ into source code. If you know how to improve goto part, let me know.

Notice: You might want to limit attempts, in order to prevent goto looping.

@rustem-art

I tried that before. But then I got the wrong encoding subject and content. It seems that encoding in ImapMailbox meeds to be UTF-8 too.

At last I do the following changes temporarily:

Before: public function searchMailbox($criteria = 'ALL') { $mailsIds = imap_search($this->getImapStream(), $criteria, SE_UID, $this->serverEncoding); return $mailsIds ? $mailsIds : array(); } After: public function searchMailbox($criteria = 'ALL', $serverEncoding = null) { $mailsIds = imap_search($this->getImapStream(), $criteria, SE_UID, $serverEncoding ?: $this->serverEncoding); return $mailsIds ? $mailsIds : array(); }

And in my app: $mailsIds = $mailbox->searchMailbox('UNSEEN', stripos($account->username, 'outlook.com') !== false ? 'US-ASCII' : 'UTF-8');

It works for me.

And sorry for my english too 😃

PhpImap\Exception [ 0 ]: Mime string encoding conversion failed ~ APPPATH/vendor/php-imap/php-imap/src/PhpImap/Mailbox.php [753]

Before:

	protected function convertStringEncoding($string, $fromEncoding, $toEncoding) {
		if(!$string || $fromEncoding == $toEncoding) {
			return $string;
		}
		...
	}

After:

	protected function convertStringEncoding($string, $fromEncoding, $toEncoding) {
		if(!$string || $fromEncoding == $toEncoding || ($fromEncoding == "utf-8" && $toEncoding == "US-ASCII")) {
			return $string;
		}
		...
	}

Explanation:

ASCII is a subset of UTF-8, so all ASCII files are already UTF-8 encoded. The bytes in the ASCII file and the bytes that would result from “encoding it to UTF-8” would be exactly the same bytes. There’s no difference between them, so there’s no need to do anything. (from StackOverflow)

To reproduce this problem you can use cyrrilic email’s subject. It will try to convertStringEncoding($subject, "utf-8", "US-ASCII") and throw exception by …

	if(!$convertedString) {
		throw new Exception('Mime string encoding conversion failed');
	}

… However function’s docblock says: @return string Converted string if conversion was successful, or the original string if not

@littleylv i can solve this problem only by add specific character param in

$mailbox = new ImapMailbox('{' . $serverAddr . ':' . $serverPort . '' . $serverSecurity . '}' . 
$serverDirectory . '', $serverLogin, $serverPass, storage_path('/tmp/'), $encoding);
            $mails = array();
....

and in your app you can select, if MS Outlook mail server then $encoding = ‘US-ASCII’; in other UTF-8…

PS sorry for my english.