Skip to content

Commit

Permalink
Issue #274: Some strings with default charset had UTF-8 characters an…
Browse files Browse the repository at this point in the history
…d failed to convert
  • Loading branch information
Sebastian Krätzig committed May 11, 2019
1 parent aeecd5d commit 04e01b9
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 3 deletions.
11 changes: 10 additions & 1 deletion src/PhpImap/DataPartInfo.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,9 @@ function fetch() {
}

switch($this->encoding) {
case ENC7BIT:
$this->data = $this->data;
break;
case ENC8BIT:
$this->data = imap_utf8($this->data);
break;
Expand All @@ -49,10 +52,16 @@ function fetch() {
case ENCQUOTEDPRINTABLE:
$this->data = quoted_printable_decode($this->data);
break;
case ENCOTHER:
$this->data = $this->data;
break;
default:
$this->data = $this->data;
break;
}

if(isset($this->charset)) {
$this->data = $this->mail->convertStringEncoding($this->data, $this->charset, $this->mail->getServerEncoding());
$this->data = $this->mail->decodeMimeStr($this->data, $this->charset);
}

return $this->data;
Expand Down
4 changes: 2 additions & 2 deletions src/PhpImap/Mailbox.php
Original file line number Diff line number Diff line change
Expand Up @@ -949,9 +949,9 @@ public function decodeMimeStr($string, $toCharset = 'utf-8') {
$newString = '';
foreach(imap_mime_header_decode($string) as $element) {
if(isset($element->text)) {
$fromCharset = !isset($element->charset) || $element->charset == 'default' ? 'iso-8859-1' : $element->charset;
$fromCharset = !isset($element->charset) ? 'iso-8859-1' : $element->charset;
// Convert to UTF-8, if string has UTF-8 characters to avoid broken strings. See https://github.com/barbushin/php-imap/issues/232
$toCharset = isset($element->charset) && $element->charset == 'UTF-8' ? 'UTF-8' : $toCharset;
$toCharset = isset($element->charset) && preg_match('/(UTF\-8)|(default)/i', $element->charset) ? 'UTF-8' : $toCharset;
$newString .= $this->convertStringEncoding($element->text, $fromCharset, $toCharset);
}
}
Expand Down
1 change: 1 addition & 0 deletions tests/unit/MailboxTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -608,6 +608,7 @@ public function testDecodeMimeStr() {
array("This is the Euro symbol ''.", "This is the Euro symbol ''."),
array('=?UTF-8?q?Some_subject_here_?= =?UTF-8?q?=F0=9F=98=98?=', 'Some subject here 😘', 'US-ASCII'),
array('=?UTF-8?Q?mountainguan=E6=B5=8B=E8=AF=95?=', 'mountainguan测试', 'US-ASCII'),
array('مقتطفات من: صن تزو. "فن الحرب". كتب أبل. Something in english', 'مقتطفات من: صن تزو. "فن الحرب". كتب أبل. Something in english', 'US-ASCII'),
);

foreach($test_strings as $test) {
Expand Down

0 comments on commit 04e01b9

Please sign in to comment.