Skip to content

Commit 53c0879

Browse files
committed
Decode MIME-encoded names in Address constructor
1 parent fd8d257 commit 53c0879

File tree

2 files changed

+21
-2
lines changed

2 files changed

+21
-2
lines changed

src/Address.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace DirectoryTree\ImapEngine;
44

5+
use DirectoryTree\ImapEngine\Support\Str;
56
use Illuminate\Contracts\Support\Arrayable;
67
use JsonSerializable;
78

@@ -13,7 +14,9 @@ class Address implements Arrayable, JsonSerializable
1314
public function __construct(
1415
protected string $email,
1516
protected string $name,
16-
) {}
17+
) {
18+
$this->name = Str::decodeMimeHeader($this->name);
19+
}
1720

1821
/**
1922
* Get the address's email.

src/Support/Str.php

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -260,7 +260,7 @@ public static function toImapUtf7(string $string): string
260260
/**
261261
* Determine if a given string matches a given pattern.
262262
*/
263-
public static function is(array|string $pattern, string $value, bool $ignoreCase = false)
263+
public static function is(array|string $pattern, string $value, bool $ignoreCase = false): bool
264264
{
265265
if (! is_iterable($pattern)) {
266266
$pattern = [$pattern];
@@ -294,4 +294,20 @@ public static function is(array|string $pattern, string $value, bool $ignoreCase
294294

295295
return false;
296296
}
297+
298+
/**
299+
* Decode MIME-encoded header values.
300+
*/
301+
public static function decodeMimeHeader(string $value): string
302+
{
303+
if (! str_contains($value, '=?')) {
304+
return $value;
305+
}
306+
307+
if ($decoded = iconv_mime_decode($value, ICONV_MIME_DECODE_CONTINUE_ON_ERROR, 'UTF-8')) {
308+
return $decoded;
309+
}
310+
311+
return $value;
312+
}
297313
}

0 commit comments

Comments
 (0)