Skip to content

Commit

Permalink
fixup! Checnge how charsets are detected for emails
Browse files Browse the repository at this point in the history
  • Loading branch information
miaulalala committed Mar 18, 2022
1 parent fac906a commit 368320d
Showing 1 changed file with 17 additions and 12 deletions.
29 changes: 17 additions & 12 deletions lib/Model/IMAPMessage.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,6 @@
use OCP\AppFramework\Db\DoesNotExistException;
use OCP\Files\File;
use OCP\Files\SimpleFS\ISimpleFile;
use ValueError;
use function in_array;
use function mb_convert_encoding;
use function mb_strcut;
Expand Down Expand Up @@ -622,23 +621,29 @@ private function loadBodyData(Horde_Mime_Part $p, $partNo): string {
$p->setContents($data);
$data = $p->getContents();

$charset = mb_detect_encoding($data, 'UTF-8', true);

if (!$charset) {
$charset = $p->getCharset();
if (empty($data)) {
return $data;
}

try {
$data = @mb_convert_encoding($data, 'UTF-8', $charset);
} catch (ValueError $e) {
throw new ServiceException('Could not detect charset for message ' . $e->getMessage(), $e->getCode());
// Try the email encoding first
$converted = @mb_convert_encoding($data, 'UTF-8', $p->getCharset());
if ($converted) {
return $converted;
}

if (!$data) {
throw new ServiceException('Could not detect charset for message');
// Fallback
$charset = mb_detect_encoding($data, null, true);
$converted = @mb_convert_encoding($data, 'UTF-8', $charset);
if ($converted) {
return $converted;
}

return $data;
// Might be a charset that PHP mb doesn't know how to handle, fall back to iconv
$converted = iconv($charset, 'UTF-8', $data);
if ($converted === false) {
throw new ServiceException('Could not detect message charset');
}
return $converted;
}

public function getContent(): string {
Expand Down

0 comments on commit 368320d

Please sign in to comment.