Skip to content

Commit a664ac5

Browse files
Fix iconv_mime_decode(): Detected an illegal character in input string - closes #5265
1 parent 3329379 commit a664ac5

File tree

4 files changed

+17
-4
lines changed

4 files changed

+17
-4
lines changed

app/Console/Commands/FetchEmails.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -713,7 +713,7 @@ public function processMessage($message, $message_id, $mailbox, $mailboxes, $ext
713713

714714
// Convert subject encoding
715715
if (preg_match('/=\?[a-z\d-]+\?[BQ]\?.*\?=/i', $subject)) {
716-
$subject = iconv_mime_decode($subject, ICONV_MIME_DECODE_CONTINUE_ON_ERROR, 'UTF-8');
716+
$subject = \Helper::iconvMimeDecode($subject);
717717
}
718718

719719
$to = $this->formatEmailList($message->getTo());

app/Misc/Helper.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2523,4 +2523,16 @@ public static function startsiWith($text, $string)
25232523
{
25242524
return (stripos($text, $string) === 0);
25252525
}
2526+
2527+
// The iconv_mime_decode() may throw an error even with ICONV_MIME_DECODE_CONTINUE_ON_ERROR.
2528+
// https://github.com/freescout-help-desk/freescout/issues/5265
2529+
public static function iconvMimeDecode($string, $mode = ICONV_MIME_DECODE_CONTINUE_ON_ERROR, $encoding = "UTF-8")
2530+
{
2531+
try {
2532+
return iconv_mime_decode($string, $mode, $encoding);
2533+
} catch (\Exception $e) {
2534+
self::logException($e);
2535+
return $string;
2536+
}
2537+
}
25262538
}

app/Misc/Mail.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -738,7 +738,7 @@ public static function imapUtf8($mime_encoded_text)
738738
if (function_exists('imap_utf8')) {
739739
return imap_utf8($mime_encoded_text);
740740
} else {
741-
return iconv_mime_decode($mime_encoded_text, ICONV_MIME_DECODE_CONTINUE_ON_ERROR, "UTF-8");
741+
return \Helper::iconvMimeDecode($mime_encoded_text);
742742
}
743743
}
744744

@@ -1288,7 +1288,7 @@ public static function decodeSubject($subject)
12881288

12891289
// iconv_mime_decode() can't decode:
12901290
// =?iso-2022-jp?B?IBskQiFaSEcyPDpuQC4wTU1qIVs3Mkp2JSIlLyU3JSItahsoQg==?=
1291-
$subject_decoded = iconv_mime_decode($subject, ICONV_MIME_DECODE_CONTINUE_ON_ERROR, "UTF-8");
1291+
$subject_decoded = \Helper::iconvMimeDecode($subject);
12921292

12931293
// Sometimes iconv_mime_decode() can't decode some parts of the subject:
12941294
// =?iso-2022-jp?B?IBskQiFaSEcyPDpuQC4wTU1qIVs3Mkp2JSIlLyU3JSItahsoQg==?=

overrides/webklex/php-imap/src/Header.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -570,7 +570,8 @@ private function decode($value) {
570570
}
571571
}
572572
} elseif ($decoder === 'iconv') {
573-
$value = iconv_mime_decode($value, ICONV_MIME_DECODE_CONTINUE_ON_ERROR, "UTF-8");
573+
//$value = iconv_mime_decode($value, ICONV_MIME_DECODE_CONTINUE_ON_ERROR, "UTF-8");
574+
$value = \Helper::iconvMimeDecode($value);
574575
} elseif ($is_utf8_base) {
575576
$value = mb_decode_mimeheader($value);
576577
}

0 commit comments

Comments
 (0)