Search code examples
phpimap

Imap php attachement file name encoding


I'm using imap php library to extract emails and save attachements.

When I wat to get attachements, I use function

$partStruct = imap_bodystruct($imap, $mailNum, $partNum);

It supposed to have name of the file in parameters attribute, but here is what I have in this attribute:

(
    [type] => 3
    [encoding] => 3
    [ifsubtype] => 1
    [subtype] => VND.OPENXMLFORMATS-OFFICEDOCUMENT.SPREADSHEETML.SHEET
    [ifdescription] => 0
    [ifid] => 0
    [bytes] => 53308
    [ifdisposition] => 1
    [disposition] => ATTACHMENT
    [ifdparameters] => 0
    [ifparameters] => 1
    [parameters] => Array
        (
            [0] => stdClass Object
                (
                    [attribute] => NAME
                    [value] => =?KOI8-R?B?4snUy8/JztkueGxzeA==?=
                )

        )

)

As I can see, it's an xlsx file, but name of the file is =?KOI8-R?B?4snUy8/JztkueGxzeA==?=

Has anyone seen that before? How do I get original utf-8 file name?

Email was sent from Imac and filename originally was in russian. I can try to decode the name, stripping =?KOI8-R?B? stuff, but it look like some kind of standard? What standard is it?


Solution

  • http://ncona.com/2011/06/using-utf-8-characters-on-an-e-mail-subject/
    https://www.ietf.org/rfc/rfc1342.txt

    So for: =?KOI8-R?B?4snUy8/JztkueGxzeA==?=

    • =? and ?= are the beginning/ending delimiters.
    • KOI8-R is the charset
    • B is for Base64 encoding, Q would denote quoted-printable encoding
    • 4snUy8/JztkueGxzeA== Is the encoded filename.