Skip to content

Commit

Permalink
Checnge how charsets are detected for emails
Browse files Browse the repository at this point in the history
Signed-off-by: Anna Larch <anna@nextcloud.com>
  • Loading branch information
miaulalala committed Jan 11, 2022
1 parent 2287909 commit fac906a
Showing 1 changed file with 19 additions and 1 deletion.
20 changes: 19 additions & 1 deletion lib/Model/IMAPMessage.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,12 @@
use OCA\Mail\Db\MailAccount;
use OCA\Mail\Db\Message;
use OCA\Mail\Db\Tag;
use OCA\Mail\Exception\ServiceException;
use OCA\Mail\Service\Html;
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 @@ -588,6 +590,7 @@ private function handleHtmlMessage(Horde_Mime_Part $p, $partNo): void {
* @return string
* @throws DoesNotExistException
* @throws Exception
* @throws ServiceException
*/
private function loadBodyData(Horde_Mime_Part $p, $partNo): string {
// DECODE DATA
Expand Down Expand Up @@ -619,7 +622,22 @@ private function loadBodyData(Horde_Mime_Part $p, $partNo): string {
$p->setContents($data);
$data = $p->getContents();

$data = mb_convert_encoding($data, 'UTF-8', $p->getCharset());
$charset = mb_detect_encoding($data, 'UTF-8', true);

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

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());
}

if (!$data) {
throw new ServiceException('Could not detect charset for message');
}

return $data;
}

Expand Down

0 comments on commit fac906a

Please sign in to comment.