Skip to content

Commit

Permalink
Header value decoding improved #410
Browse files Browse the repository at this point in the history
  • Loading branch information
Webklex committed Jun 23, 2023
1 parent d3b9eaf commit 9d6a2d5
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 7 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ Updates should follow the [Keep a CHANGELOG](http://keepachangelog.com/) princip
## [UNRELEASED]
### Fixed
- Legacy protocol support fixed (object to array conversion) #411
- Header value decoding improved #410

### Added
- NaN
Expand Down
14 changes: 8 additions & 6 deletions src/Header.php
Original file line number Diff line number Diff line change
Expand Up @@ -421,22 +421,24 @@ public function decode(mixed $value): mixed {
$decoder = $this->config['decoder']['message'];

if ($value !== null) {
if ($decoder === 'utf-8' && extension_loaded('imap')) {
if ($decoder === 'utf-8') {
$decoded_values = $this->mime_header_decode($value);
$tempValue = "";
foreach ($decoded_values as $decoded_value) {
$tempValue .= $this->convertEncoding($decoded_value->text, $decoded_value->charset);
}
if ($tempValue) {
$value = $tempValue;
} else {
} else if (extension_loaded('imap')) {
$value = \imap_utf8($value);
}else if (function_exists('iconv_mime_decode')){
$value = iconv_mime_decode($value, ICONV_MIME_DECODE_CONTINUE_ON_ERROR, "UTF-8");
}else{
$value = mb_decode_mimeheader($value);
}
} elseif ($decoder === 'iconv' && $this->is_uft8($value)) {
}elseif ($decoder === 'iconv') {
$value = iconv_mime_decode($value, ICONV_MIME_DECODE_CONTINUE_ON_ERROR, "UTF-8");
}

if ($this->is_uft8($value)) {
}else if ($this->is_uft8($value)) {
$value = mb_decode_mimeheader($value);
}

Expand Down
2 changes: 1 addition & 1 deletion tests/fixtures/ImapMimeHeaderDecodeReturnsFalseTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ class ImapMimeHeaderDecodeReturnsFalseTest extends FixtureTestCase {
public function testFixture() : void {
$message = $this->getFixture("imap_mime_header_decode_returns_false.eml");

self::assertEquals("?p?#]ݰ?[??W̌N??LL?̍L??NL˜", $message->subject->first());
self::assertEquals("=?UTF-8?B?nnDusSNdG92w6Fuw61fMjAxOF8wMy0xMzMyNTMzMTkzLnBkZg==?=", $message->subject->first());
self::assertEquals("Hi", $message->getTextBody());
self::assertFalse($message->hasHTMLBody());
self::assertEquals("2017-09-13 11:05:45", $message->date->first()->setTimezone('UTC')->format("Y-m-d H:i:s"));
Expand Down
27 changes: 27 additions & 0 deletions tests/issues/Issue410Test.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php
/*
* File: Issue410Test.php
* Category: -
* Author: M.Goldenbaum
* Created: 23.06.23 20:41
* Updated: -
*
* Description:
* -
*/

namespace Tests\issues;

use PHPUnit\Framework\TestCase;
use Webklex\PHPIMAP\Message;

class Issue410Test extends TestCase {

public function testIssueEmail() {
$filename = implode(DIRECTORY_SEPARATOR, [__DIR__, "..", "messages", "issue-410.eml"]);
$message = Message::fromFile($filename);

self::assertSame("☆第132号 「ガーデン&エクステリア」専門店のためのQ&Aサロン 【月刊エクステリア・ワーク】", (string)$message->subject);
}

}
9 changes: 9 additions & 0 deletions tests/messages/issue-410.eml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
From: [email protected]
To: [email protected]
Subject: =?ISO-2022-JP?B?GyRCIXlCaBsoQjEzMhskQjlmISEhViUsITwlRyVzGyhCJhskQiUoJS8lOSVGJWolIiFXQGxMZ0U5JE4kPyRhJE4jURsoQiYbJEIjQSU1JW0lcyEhIVo3bjQpJSglLyU5JUYlaiUiISYlbyE8JS8hWxsoQg==?=
Date: Wed, 13 Sep 2017 13:05:45 +0200
Content-Type: text/plain;
charset="us-ascii"
Content-Transfer-Encoding: quoted-printable

Hi

0 comments on commit 9d6a2d5

Please sign in to comment.