Skip to content
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
6 changes: 3 additions & 3 deletions src/PhpImap/Mailbox.php
Original file line number Diff line number Diff line change
Expand Up @@ -2027,7 +2027,7 @@ protected function possiblyGetEmailAndNameFromRecipient(object $recipient): ?arr
}

if ('' !== \trim($recipientMailbox) && '' !== \trim($recipientHost)) {
$recipientEmail = \strtolower($recipientMailbox.'@'.$recipientHost);
$recipientEmail = \mb_strtolower($recipientMailbox.'@'.$recipientHost, 'UTF-8');
$recipientName = (\is_string($recipientPersonal) && '' !== \trim($recipientPersonal)) ? $this->decodeMimeStr($recipientPersonal) : null;

return [
Expand Down Expand Up @@ -2105,7 +2105,7 @@ protected function possiblyGetHostNameAndAddress(array $t): array
}

/** @var string */
$out[] = \strtolower($t[0]->mailbox.'@'.(string) $out[0]);
$out[] = \mb_strtolower($t[0]->mailbox.'@'.(string) $out[0], 'UTF-8');

/** @var array{0:string|null, 1:string|null, 2:string} */
return $out;
Expand Down Expand Up @@ -2142,7 +2142,7 @@ protected function searchMailboxFromWithOrWithoutDisablingServerEncoding(string
* @return string
*/
static function ($sender) use ($criteria): string {
return $criteria.' FROM '.\mb_strtolower($sender);
return $criteria.' FROM '.\mb_strtolower($sender, 'UTF-8');
},
$senders
)));
Expand Down
18 changes: 18 additions & 0 deletions tests/unit/Fixtures/Mailbox.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,24 @@ public function decodeRFC2231ForTests(string $string): string
return $this->decodeRFC2231($string);
}

/**
* @return (null|string)[]|null
*/
public function possiblyGetEmailAndNameFromRecipientForTests(object $recipient): ?array
{
return $this->possiblyGetEmailAndNameFromRecipient($recipient);
}

/**
* @param array<int, object> $t
*
* @return array{0:string|null, 1:string|null, 2:string}
*/
public function possiblyGetHostNameAndAddressForTests(array $t): array
{
return $this->possiblyGetHostNameAndAddress($t);
}

public function getImapPassword(): string
{
return $this->imapPassword;
Expand Down
103 changes: 103 additions & 0 deletions tests/unit/MailboxAddressParsingTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
<?php
/**
* Mailbox address parsing focused unit tests.
*/
declare(strict_types=1);

namespace PhpImap;

use PHPUnit\Framework\TestCase;
use stdClass;

final class MailboxAddressParsingTest extends TestCase
{
public function testPossiblyGetEmailAndNameFromRecipientUsesMultibyteSafeLowercasing(): void
{
$recipient = new stdClass();
$recipient->mailbox = 'FÜR.MICH';
$recipient->host = 'EXAMPLE.DE';

$mailbox = new Fixtures\Mailbox('', '', '');

$this->assertSame(
['für.mich@example.de', null],
$mailbox->possiblyGetEmailAndNameFromRecipientForTests($recipient)
);
}

public function testPossiblyGetHostNameAndAddressUsesMultibyteSafeLowercasing(): void
{
$sender = new stdClass();
$sender->mailbox = 'GRÜNE.POST';
$sender->host = 'EXAMPLE.DE';

$mailbox = new Fixtures\Mailbox('', '', '');

$this->assertSame(
['EXAMPLE.DE', null, 'grüne.post@example.de'],
$mailbox->possiblyGetHostNameAndAddressForTests([$sender])
);
}

public function testSearchMailboxFromLowercasesSendersUsingUtf8RegardlessOfInternalEncoding(): void
{
$mailbox = new class('', '', '') extends Fixtures\Mailbox {
/** @var array{disableServerEncoding: bool, criteria: string[]} */
public array $capturedSearchMailboxArguments = [];

public function searchMailboxFromWithOrWithoutDisablingServerEncodingForTests(string $criteria, bool $disableServerEncoding, string $sender, string ...$senders): array
{
return $this->searchMailboxFromWithOrWithoutDisablingServerEncoding($criteria, $disableServerEncoding, $sender, ...$senders);
}

/**
* @param bool $disableServerEncoding
* @param string $single_criteria
* @param string ...$criteria
*
* @return int[]
*/
protected function searchMailboxMergeResultsWithOrWithoutDisablingServerEncoding($disableServerEncoding, $single_criteria, ...$criteria)
{
\array_unshift($criteria, $single_criteria);

$this->capturedSearchMailboxArguments = [
'disableServerEncoding' => $disableServerEncoding,
'criteria' => $criteria,
];

return [];
}
};

$previousInternalEncoding = \mb_internal_encoding();

try {
\mb_internal_encoding('ISO-8859-1');

$this->assertSame(
[],
$mailbox->searchMailboxFromWithOrWithoutDisablingServerEncodingForTests(
'ALL',
true,
'FÜR@EXAMPLE.DE',
'für@example.de',
'NOREPLY@EXAMPLE.DE'
)
);
} finally {
\mb_internal_encoding($previousInternalEncoding);
}

$this->assertSame(
[
'disableServerEncoding' => true,
'criteria' => [
'ALL FROM für@example.de',
'ALL FROM noreply@example.de',
],
],
$mailbox->capturedSearchMailboxArguments
);
}
}
Loading