Skip to content

Commit 78b2f38

Browse files
authored
Merge pull request #141 from DirectoryTree/bug-139
Bug 139 - =?UTF-8?B?[.....]==?= encoded string in From name
2 parents fd8d257 + 76c50b1 commit 78b2f38

File tree

3 files changed

+107
-2
lines changed

3 files changed

+107
-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
}

tests/Unit/FileMessageTest.php

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -547,3 +547,89 @@
547547

548548
expect($message->size())->toBe(strlen($contents));
549549
});
550+
551+
test('it decodes base64 encoded from name', function () {
552+
$contents = <<<'EOT'
553+
Return-Path: <[email protected]>
554+
Delivered-To: [email protected]
555+
Received: from mail.example.com
556+
by mail.example.com with LMTP id kB9HBDDVNWiAaAAABEj0/g
557+
for <[email protected]>; Tue, 27 May 2025 17:07:28 +0200
558+
Received: from exmaple.com (exmaple.com [1.1.1.1])
559+
by mail.example.com (Postfix) with ESMTPS id E15722D749
560+
for <[email protected]>; Tue, 27 May 2025 17:07:27 +0200 (CEST)
561+
DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=example.com;
562+
s=default; h=Content-Transfer-Encoding:Content-Type:Subject:MIME-Version:
563+
Message-Id:To:From:Date:Sender:Reply-To:Cc:Content-ID:Content-Description:
564+
Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID:
565+
In-Reply-To:References:List-Id:List-Help:List-Unsubscribe:List-Subscribe:
566+
List-Post:List-Owner:List-Archive;
567+
bh=v6FVS3RZyBFj+9PZnaE1NGLHzvCYTYn6axfEJnK9FUQ=; b=ab91Cc8m1EOAClyNW066JVKy7p
568+
Kq6C6Kg84UvJ2CqW6He0BJY1SuK7s9HfzzQA+32N9YVn8HS9tNyynJ3MP0tUa/zf2YR4H7FRO/afz
569+
hpxcpJYVJGaH2rCgWzubZsKPJQN5wzd5g5pRiT4LbNchTr1XfwlVX3ycRxun+hGXNSNVJC8cSQuul
570+
tdZnKp69+ngVh0fFzzuywpj29Lmeh3hplsF3KlP23UMZq+5ZSp9WNEBPyB/dXG3XNuqqNQGqlHGoy
571+
xVOEHWA6r11YJxw+6W2vU3qVg6iW/Uy8g+bdp/AkULoqlsiBGR9of+/zozaobt7EkR0A7JTMKRUKw
572+
Xw5WUunA==;
573+
Received: from 1.1.1.1.example.com ([1.1.1.1]:65442 helo=SERVER1)
574+
by exmaple.com with esmtpsa (TLS1.2) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384
575+
(Exim 4.98.1)
576+
(envelope-from <[email protected]>)
577+
id 1uJvto-000000000fL-1qpV;
578+
Tue, 27 May 2025 17:07:27 +0200
579+
Date: Tue, 27 May 2025 17:07:27 +0200
580+
From: "=?UTF-8?B?REFQIHwgU0VSVkVSMQ==?=" <[email protected]>
581+
582+
Message-Id: <[email protected]>
583+
MIME-Version: 1.0
584+
Subject: =?UTF-8?B?W0RBUCB8IFNFUlZFUjFdICBDb250YWluZXIgdmF1bHR3YXJkZW4gaW4gQ29udGFpbmVyIE1hbmFnZXIgc3RvcHBlZCB1bmV4cGVjdGVkbHk=?=
585+
Content-Type: text/html; charset=utf-8
586+
Content-Transfer-Encoding: 8bit
587+
X-AntiAbuse: This header was added to track abuse, please include it with any abuse report
588+
X-AntiAbuse: Primary Hostname - exmaple.com
589+
X-AntiAbuse: Original Domain - example.com
590+
X-AntiAbuse: Originator/Caller UID/GID - [47 12] / [47 12]
591+
X-AntiAbuse: Sender Address Domain - example.com
592+
X-Get-Message-Sender-Via: exmaple.com: authenticated_id: [email protected]
593+
X-Authenticated-Sender: exmaple.com: [email protected]
594+
X-Rspamd-Server: mail.example.com
595+
X-Spamd-Result: default: False [-0.91 / 6.00];
596+
SUBJ_EXCESS_BASE64(1.50)[];
597+
FROM_EXCESS_BASE64(1.50)[];
598+
IP_REPUTATION_HAM(-1.32)[asn: 20857(-0.38), country: NL(-0.01), ip: 1.1.1.1(-0.93)];
599+
GENERIC_REPUTATION(-0.95)[-0.94652652518558];
600+
DKIM_REPUTATION(-0.92)[-0.91631218832379];
601+
SPF_REPUTATION_HAM(-0.89)[-0.88873676028191];
602+
R_DKIM_ALLOW(0.29)[example.com:s=default];
603+
R_SPF_ALLOW(-0.20)[+mx];
604+
MIME_HTML_ONLY(0.20)[];
605+
BAYES_HAM(-0.12)[66.55%];
606+
MX_GOOD(-0.01)[];
607+
TO_MATCH_ENVRCPT_SOME(0.00)[];
608+
FROM_HAS_DN(0.00)[];
609+
NEURAL_HAM(-0.00)[-1.000];
610+
HAS_X_GMSV(0.00)[[email protected]];
611+
DMARC_NA(0.00)[example.com];
612+
RCVD_VIA_SMTP_AUTH(0.00)[];
613+
MIME_TRACE(0.00)[0:~];
614+
MID_RHS_MATCH_FROM(0.00)[];
615+
DKIM_TRACE(0.00)[example.com:+];
616+
RCPT_COUNT_TWO(0.00)[2];
617+
TO_DN_NONE(0.00)[];
618+
HAS_X_AS(0.00)[[email protected]];
619+
HAS_X_ANTIABUSE(0.00)[];
620+
RCVD_COUNT_TWO(0.00)[2];
621+
FROM_EQ_ENVFROM(0.00)[];
622+
ASN(0.00)[asn:20857, ipnet:37.97.128.0/17, country:NL];
623+
RCVD_TLS_ALL(0.00)[]
624+
X-Rspamd-Queue-Id: E15722D749
625+
X-EsetId: 37303A29E962D55F607264
626+
627+
Container vaultwarden in Container Manager stopped unexpectedly. Please select vaultwarden on the <b>Container</b> page, click the <b>Details</b> button, and go to the <b>Log</b> tab for details.<BR><BR>From SERVER1<BR><BR><BR>
628+
EOT;
629+
630+
$message = new FileMessage($contents);
631+
632+
expect($message->from()->email())->toBe('[email protected]');
633+
expect($message->from()->name())->toBe('DAP | SERVER1');
634+
expect($message->subject())->toBe('[DAP | SERVER1] Container vaultwarden in Container Manager stopped unexpectedly');
635+
});

0 commit comments

Comments
 (0)