Skip to content
This repository has been archived by the owner on Jan 30, 2020. It is now read-only.

fix header encode run into iconv Unknown error (7) #78

Merged
merged 1 commit into from
Apr 11, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 15 additions & 1 deletion src/Header/HeaderWrap.php
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,21 @@ public static function mimeDecodeValue($value)
*/
public static function canBeEncoded($value)
{
$encoded = iconv_mime_encode('x-test', $value, ['scheme' => 'Q']);
// avoid any wrapping by specifying line length long enough
// "test" -> 4
// "x-test: =?ISO-8859-1?B?dGVzdA==?=" -> 33
// 8 +2 +3 +3 -> 16
$charset = 'UTF-8';
$line_length = strlen($value) * 4 + strlen($charset) + 16;

$preferences = [
'scheme' => 'Q',
'input-charset' => $charset,
'output-charset' => $charset,
'line-length' => $line_length,
];

$encoded = iconv_mime_encode('x-test', $value, $preferences);

return (false !== $encoded);
}
Expand Down
21 changes: 21 additions & 0 deletions test/Header/HeaderWrapTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

namespace ZendTest\Mail\Header;

use Zend\Mail\Header\GenericHeader;
use Zend\Mail\Header\HeaderWrap;

/**
Expand Down Expand Up @@ -70,4 +71,24 @@ public function testMimeDecoding()

$this->assertEquals($expected, $decoded);
}

/**
* Test that fails with HeaderWrap::canBeEncoded at lowest level:
* iconv_mime_encode(): Unknown error (7)
*
* which can be triggered as:
* $header = new GenericHeader($name, $value);
*/
public function testCanBeEncoded()
{
$name = 'Subject';
$value = "[#77675] New Issue:xxxxxxxxx xxxxxxx xxxxxxxx xxxxxxxxxxxxx xxxxxxxxxx xxxxxxxx, tähtaeg xx.xx, xxxx";
$encoded = "Subject: =?UTF-8?Q?[#77675]=20New=20Issue:xxxxxxxxx=20xxxxxxx=20xxxxxxxx=20?=\r\n =?UTF-8?Q?xxxxxxxxxxxxx=20xxxxxxxxxx=20xxxxxxxx,=20t=C3=A4htaeg=20xx.xx,=20xxxx?=";
$res = HeaderWrap::canBeEncoded($value);
$this->assertTrue($res);

$header = new GenericHeader($name, $value);
$res = $header->toString();
$this->assertEquals($encoded, $res);
}
}