Skip to content

Commit

Permalink
Use "BADCHARSET" exception to determine the required charset (#100)
Browse files Browse the repository at this point in the history
  • Loading branch information
Webklex committed Aug 31, 2020
1 parent d27f53c commit 96eb056
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 6 deletions.
4 changes: 2 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,13 @@ Updates should follow the [Keep a CHANGELOG](http://keepachangelog.com/) princip
- NaN

### Added
- NaN
- "BADCHARSET" exception will be used to determine the required charset (#100)

### Breaking changes
- NaN

### Affected Classes
- NaN
- [Query::class](src/Query/Query.php)

## [1.5.3] - 2020-08-24
### Fixed
Expand Down
23 changes: 19 additions & 4 deletions src/IMAP/Query/Query.php
Original file line number Diff line number Diff line change
Expand Up @@ -148,15 +148,30 @@ public function markAsRead() {
*/
protected function search(){
$this->generate_query();
$available_messages = [];

/**
* Don't set the charset if it isn't used - prevent strange outlook mail server errors
* @see https://github.com/Webklex/laravel-imap/issues/100
*
* If a "BADCHARSET" error gets caught it will be used to determine the desired charset.
*/
if($this->getCharset() === null){
$available_messages = \imap_search($this->getClient()->getConnection(), $this->getRawQuery(), IMAP::SE_UID);
}else{
$available_messages = \imap_search($this->getClient()->getConnection(), $this->getRawQuery(), IMAP::SE_UID, $this->getCharset());
try {
if ($this->getCharset() == null) {
$available_messages = \imap_search($this->getClient()->getConnection(), $this->getRawQuery(), IMAP::SE_UID);
}else{
$available_messages = \imap_search($this->getClient()->getConnection(), $this->getRawQuery(), IMAP::SE_UID, $this->getCharset());
}
} catch (\Exception $e) {
if (strpos($e, ' [BADCHARSET (')) {
preg_match('/ \[BADCHARSET \((.*)\)\]/', $e, $matches);
if (isset($matches[1])) {
if ($matches[1] !== $this->getCharset()){
$this->setCharset($matches[1]);
return $this->search();
}
}
}
}

if ($available_messages !== false) {
Expand Down

0 comments on commit 96eb056

Please sign in to comment.