Skip to content

Commit

Permalink
Fix decoding Japanese symbols in the email body in gb2312 encoding - c…
Browse files Browse the repository at this point in the history
…loses #4282
  • Loading branch information
freescout-help-desk committed Oct 17, 2024
1 parent 5db8906 commit 4e8cd07
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 7 deletions.
22 changes: 22 additions & 0 deletions app/Misc/Mail.php
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,15 @@ class Mail
'-------- Αρχικό μήνυμα --------', // Greek
];

/**
* Used to substitue encoding during mail body decoding.
* https://github.com/freescout-help-desk/freescout/issues/4282
*/
public static $encoding_substitution = [
'iso-2022-jp' => 'iso-2022-jp-ms',
'gb2312' => 'gb18030',
];

/**
* md5 of the last applied mail config.
*/
Expand Down Expand Up @@ -1142,6 +1151,19 @@ public static function parseEml($content, $mailbox) {
return \Webklex\PHPIMAP\Message::make(null, null, $client, $raw_header, $raw_body, [], \Webklex\PHPIMAP\IMAP::ST_UID);
}

// Substitue encoding during mail body decoding.
// https://github.com/freescout-help-desk/freescout/issues/4282
public static function substituteEncoding($encoding)
{
$encoding = strtolower($encoding);

if (!empty(self::$encoding_substitution[$encoding])) {
return self::$encoding_substitution[$encoding];
} else {
return $encoding;
}
}

// public static function oauthGetProvider($provider_code, $params)
// {
// $provider = null;
Expand Down
4 changes: 1 addition & 3 deletions overrides/webklex/php-imap/src/Header.php
Original file line number Diff line number Diff line change
Expand Up @@ -431,9 +431,7 @@ public function convertEncoding($str, $from = "ISO-8859-2", $to = "UTF-8") {
return $str;
}

if (strtolower($from) == 'iso-2022-jp'){
$from = 'iso-2022-jp-ms';
}
$from = \MailHelper::substituteEncoding($from);

try {
if (function_exists('iconv') && $from != 'UTF-7' && $to != 'UTF-7') {
Expand Down
6 changes: 2 additions & 4 deletions overrides/webklex/php-imap/src/Message.php
Original file line number Diff line number Diff line change
Expand Up @@ -749,10 +749,8 @@ public function convertEncoding($str, string $from = "ISO-8859-2", string $to =

$result = '';

if (strtolower($from) == 'iso-2022-jp'){
$from = 'iso-2022-jp-ms';
}

$from = \MailHelper::substituteEncoding($from);

// Try iconv.
if (function_exists('iconv') && $from != 'UTF-7' && $to != 'UTF-7' && $from != 'iso-2022-jp-ms') {
try {
Expand Down

0 comments on commit 4e8cd07

Please sign in to comment.