Skip to content

Commit 0fdd8f2

Browse files
committed
Encoding / Decoding improved and exception fallback added
1 parent 7560daf commit 0fdd8f2

File tree

2 files changed

+20
-8
lines changed

2 files changed

+20
-8
lines changed

CHANGELOG.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ Updates should follow the [Keep a CHANGELOG](http://keepachangelog.com/) princip
66

77
## [UNRELEASED]
88
### Fixed
9-
- NaN
9+
- Encoding / Decoding improved and exception fallback added
1010

1111
### Added
1212
- NaN
@@ -15,7 +15,7 @@ Updates should follow the [Keep a CHANGELOG](http://keepachangelog.com/) princip
1515
- NaN
1616

1717
### Affected Classes
18-
- NaN
18+
- [Message::class](src/Message.php)
1919

2020
## [1.6.2] - 2020-09-07
2121
### Fixed

src/IMAP/Message.php

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -386,6 +386,9 @@ private function parseHeader() {
386386
if (property_exists($header, 'subject')) {
387387
if($this->config['decoder']['message']['subject'] === 'utf-8') {
388388
$this->subject = \imap_utf8($header->subject);
389+
if (Str::startsWith(mb_strtolower($this->subject), '=?utf-8?')) {
390+
$this->subject = mb_decode_mimeheader($header->subject);
391+
}
389392
}elseif($this->config['decoder']['message']['subject'] === 'iconv') {
390393
$this->subject = iconv_mime_decode($header->subject);
391394
}else{
@@ -866,13 +869,22 @@ public function convertEncoding($str, $from = "ISO-8859-2", $to = "UTF-8") {
866869
return $str;
867870
}
868871

869-
if (function_exists('iconv') && $from != 'UTF-7' && $to != 'UTF-7') {
870-
return @iconv($from, $to.'//IGNORE', $str);
871-
} else {
872-
if (!$from) {
873-
return mb_convert_encoding($str, $to);
872+
try {
873+
if (function_exists('iconv') && $from != 'UTF-7' && $to != 'UTF-7') {
874+
return iconv($from, $to, $str);
875+
} else {
876+
if (!$from) {
877+
return mb_convert_encoding($str, $to);
878+
}
879+
return mb_convert_encoding($str, $to, $from);
880+
}
881+
} catch (\Exception $e) {
882+
if (strstr($from, '-')) {
883+
$from = str_replace('-', '', $from);
884+
return $this->convertEncoding($str, $from, $to);
885+
} else {
886+
return $str;
874887
}
875-
return mb_convert_encoding($str, $to, $from);
876888
}
877889
}
878890

0 commit comments

Comments
 (0)