ddeboer / imap

Object-oriented, fully tested PHP IMAP library

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

mb_convert_encoding(): Illegal character encoding specified

ivanghisleni opened this issue · comments

Q A
ddeboer/imap version 1.10.0
PHP version 7.4.29
IMAP provider -

Summary

I have a problem importing some mail with header with unsupported cherset which block the import, I'm asking if is it possible to handle the exception thown into src/Message/Transcoder.php::decode() maybe into src/Message/Headers.php::__constructor() with try catch with safely ignore?

Here a sample how should be fixed:

final class Headers extends Parameters
{
    /**
     * Constructor.
     */
    public function __construct(\stdClass $headers)
    {
        parent::__construct();

        // Store all headers as lowercase
        $headers = \array_change_key_case((array) $headers);

        foreach ($headers as $key => $value) {
            try {
                $this[$key] = $this->parseHeader($key, $value);
            } catch (\Exception $e) {
                // safely skip header with Unsupported charset
            }
        }
    }

Current behavior

Unsupported charset "": mb_convert_encoding(): Illegal character encoding specified
imap_alerts (0):
imap_errors (0):

How to reproduce: code & error stack trace

this is a sample of EML causing the issue:
[ Header "Subject" is the bugged header ]

X-Real-To: <monitor@bla.bla>
X-Stored-In: BlaBla
Return-Path: <info@bla.bla>
Received: from <postmaster@bla.bla>
  by bla.bla (CommuniGate Pro RULE 6.1.13)
  with RULE id 14057804; Mon, 27 Feb 2017 13:21:44 +0930
X-Autogenerated: Mirror
Resent-From: <postmaster@bla.bla>
Resent-Date: Mon, 27 Feb 2017 13:21:44 +0930

Message-Id: <201702270351.BGF77614@bla.bla>
From: =?X-IAS-German?B?bXlHb3Y=?=<info@bla.bla>
To: sales@bla.bla
Subject: =?X-IAS-German?B?U3VibWl0IHlvdXIgdGF4IHJlZnVuZCB8IEF1c3RyYWxpYW4gVGF4YXRpb24gT2ZmaWNlLg==?=
Date: 27 Feb 2017 04:51:29 +0100
MIME-Version: 1.0
Content-Type: text/html;
        charset="iso-8859-1"
Content-Transfer-Encoding: quoted-printable

)

Expected behavior

Handle the error conversion maybe skipping the not supported header.

From: =?X-IAS-German?B?bXlHb3Y=?=<info@bla.bla>

Is this the original header that caused the issue? If not, may you post the original one please?

From: =?X-IAS-German?B?bXlHb3Y=?=<info@bla.bla>

Is this the original header that caused the issue? If not, may you post the original one please?

Yes, I only replaced the original domain with bla.bla, Subject is original and not modified.

What the hell is X-IAS-German which doesn't exist in Google https://www.google.com/search?q=%22X-IAS-German%22 🤷

Your fix looks promising, can you open a PR with a test case along the fix please?