Skip to content
Open
Show file tree
Hide file tree
Changes from 4 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
13 changes: 13 additions & 0 deletions src/MailCollector.php
Original file line number Diff line number Diff line change
Expand Up @@ -2382,6 +2382,19 @@ public function getDecodedContent(Part $part)
}

$charset = $content_type->getParameter('charset');

// If charset is not specified, try to detect it automatically
if ($charset === null) {
// Use mb_detect_encoding to guess the charset
$detected_charset = mb_detect_encoding($contents, ['UTF-8', 'ISO-8859-1', 'ISO-8859-15', 'Windows-1252'], true);
if ($detected_charset !== false) {
$charset = $detected_charset;
} else {
// Fallback to ISO-8859-1 as it's the most common for mail headers without charset
$charset = 'ISO-8859-1';
}
}

if ($charset !== null && strtoupper($charset) != 'UTF-8') {
/* mbstring functions do not handle the 'ks_c_5601-1987' &
* 'ks_c_5601-1989' charsets. However, these charsets are used, for
Expand Down
25 changes: 25 additions & 0 deletions tests/emails-tests/47-missing-charset.eml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
From: [email protected]
To: [email protected]
Subject: System Report
MIME-Version: 1.0
Content-Type: text/plain
Content-Transfer-Encoding: quoted-printable
Date: Mon, 21 Oct 2025 10:00:00 +0000
Message-ID: <[email protected]>

ATTENTION: Ne cliquez pas sur les liens ou n'ouvrez pas les pi=E8ces jointe=
s si vous n'=EAtes pas s=FBr du contenu.

This is a test message with:
- Accented characters: =E8 =EA =FB (è ê û in ISO-8859-1)
- Quote printable encoding
- NO charset parameter in Content-Type header

The bug is that when charset is missing, these bytes are not converted to UTF-8.

Details:
=E8 (è), =E9 (é), =EA (ê), =EB (ë)
=E0 (à), =E2 (â), =E4 (ä)
=F9 (ù), =FB (û), =FC (ü)

This should be handled correctly by mb_detect_encoding() fallback.
21 changes: 21 additions & 0 deletions tests/imap/MailCollectorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
use ITILFollowup;
use Laminas\Mail\Protocol\Imap;
use Laminas\Mail\Protocol\Pop3;
use Laminas\Mail\Storage\Mbox;
use Laminas\Mail\Storage\Message;
use NotificationTarget;
use NotificationTargetSoftwareLicense;
Expand Down Expand Up @@ -1493,4 +1494,24 @@ public function testCleanContent(
$result = $mailcollector->cleanContent($original);
$this->assertEquals($expected, $result);
}

public function testMissingCharsetMailIsProcessed()
{
$email_file = GLPI_ROOT . '/tests/emails-tests/47-missing-charset.eml';
$this->assertFileExists($email_file);

$storage = new Mbox(['filename' => $email_file]);
$message = $storage->current();

$body_text = null;
foreach ($message as $part) {
if ($part->getHeader('content-type')->getType() === 'text/plain') {
$body_text = $part->getContent();
break;
}
}

$this->assertNotNull($body_text, 'No text/plain part found in email');
$this->assertStringContainsString('ATTENTION', $body_text);
}
}
Loading