diff --git a/src/PhpImap/DataPartInfo.php b/src/PhpImap/DataPartInfo.php index b02c1c94..173d1e59 100644 --- a/src/PhpImap/DataPartInfo.php +++ b/src/PhpImap/DataPartInfo.php @@ -109,7 +109,8 @@ protected function convertEncodingAfterFetch(): string { if (isset($this->charset) and !empty(\trim($this->charset))) { $this->data = $this->mail->decodeMimeStr( - (string) $this->data // Data to convert + (string) $this->data, // Data to convert + (string) \trim($this->charset) ); } diff --git a/src/PhpImap/Mailbox.php b/src/PhpImap/Mailbox.php index a50befbf..5cdfc933 100644 --- a/src/PhpImap/Mailbox.php +++ b/src/PhpImap/Mailbox.php @@ -1333,6 +1333,7 @@ public function downloadAttachment(DataPartInfo $dataInfo, array $params, object * Decodes a mime string. * * @param string $string MIME string to decode + * @param string|null $charsetPreferred The charset of the data part info if it was set * * @return string Converted string if conversion was successful, or the original string if not * @@ -1340,7 +1341,7 @@ public function downloadAttachment(DataPartInfo $dataInfo, array $params, object * * @todo update implementation pending resolution of https://github.com/vimeo/psalm/issues/2619 & https://github.com/vimeo/psalm/issues/2620 */ - public function decodeMimeStr(string $string): string + public function decodeMimeStr(string $string, ?string $charsetPreferred = null): string { $newString = ''; /** @var list|false */ @@ -1354,7 +1355,7 @@ public function decodeMimeStr(string $string): string $charset = \strtolower($element->charset); if ('default' === $charset) { - $charset = $this->decodeMimeStrDefaultCharset; + $charset = !empty($charsetPreferred) ? $charsetPreferred : $this->decodeMimeStrDefaultCharset; } switch ($charset) {