Search code examples
phpmimerfc2231

How can I decode a string that has been encoded using RFC2231 using PHP


I am parsing MIME formatted emails which have attachments. The attachments have their filenames encoded using RFC2231, so for example the 'content-disposition' of one of the attached files is:

attachment;  filename*=utf-8''Bill%20Sixteen.pdf

How do I go about decoding that filename?


Solution

  • Here is the specific method out of the akelos framework as mentioned by Dukeatcoding.

    /**
     * RFC 2231 Implementation
     */
    public function _decodeHeaderAttribute($header_attribute, $charset = '')
    {
        if(preg_match("/^([A-Z0-9\-]+)(\'[A-Z\-]{2,5}\')?/i",$header_attribute,$match)){
            $charset = $match[1];
            $header_attribute = urldecode(str_replace(array('_','='),array('%20','%'), substr($header_attribute,strlen($match[0]))));
        }
        return Ak::recode($header_attribute, 'UTF-8', $charset);
    }
    

    See how they do it and create your own decode function using that ;)

    P.S. I believe that the akelos framework uses the LGLP license - so be warned should you use that method as is in your own project.