Search code examples
phputf-8imap

PHP IMAP Decode UTF-8 email body


I have trouble with decoding email body with PHP. I have 2 UTF-8 emails, which structures are identical. For one I can decode the body with iconv("UTF-8", "Windows-1252", $data) For the other one this doesn't work. 1) I don't know what to use instead of this 2) How can I identified differentiate the 2 types of emails dynamically ?

stdClass Object
(
    [type] => 1
    [encoding] => 0
    [ifsubtype] => 1
    [subtype] => MIXED
    [ifdescription] => 0
    [ifid] => 0
    [ifdisposition] => 0
    [ifdparameters] => 0
    [ifparameters] => 1
    [parameters] => Array
        (
            [0] => stdClass Object
                (
                    [attribute] => BOUNDARY
                    [value] => ----=_Part_8432_30170987.1457360103746
                )
        )

    [parts] => Array
        (
            [0] => stdClass Object
                (
                    [type] => 0
                    [encoding] => 4
                    [ifsubtype] => 1
                    [subtype] => PLAIN
                    [ifdescription] => 0
                    [ifid] => 0
                    [lines] => 50
                    [bytes] => 1811
                    [ifdisposition] => 0
                    [ifdparameters] => 0
                    [ifparameters] => 1
                    [parameters] => Array
                        (
                            [0] => stdClass Object
                                (
                                    [attribute] => CHARSET
                                    [value] => UTF-8
                                )

                        )

                )



        )

)


stdClass Object
(
    [type] => 1
    [encoding] => 0
    [ifsubtype] => 1
    [subtype] => MIXED
    [ifdescription] => 0
    [ifid] => 0
    [ifdisposition] => 0
    [ifdparameters] => 0
    [ifparameters] => 1
    [parameters] => Array
        (
            [0] => stdClass Object
                (
                    [attribute] => BOUNDARY
                    [value] => ----=_Part_1135_19111967.1451925068339
                )

        )
    [parts] => Array
        (
            [0] => stdClass Object
                (
                    [type] => 0
                    [encoding] => 4
                    [ifsubtype] => 1
                    [subtype] => PLAIN
                    [ifdescription] => 0
                    [ifid] => 0
                    [lines] => 27
                    [bytes] => 490
                    [ifdisposition] => 0
                    [ifdparameters] => 0
                    [ifparameters] => 1
                    [parameters] => Array
                        (
                            [0] => stdClass Object
                                (
                                    [attribute] => CHARSET
                                    [value] => UTF-8
                                )
                        )
                )
        )
)

When I use imap_mime_header_decode on both data objects I get this :

Array
(
    [0] => stdClass Object
        (
            [charset] => default
            [text] =>
...
)

Thanks in advance, Stiti


Solution

  • Problem solved : I used :

    iconv("UTF-8", "Windows-1252//TRANSLIT//IGNORE", $data)
    

    instead of :

    iconv("UTF-8", "Windows-1252", $data)
    

    And it works great now.