2

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?

Nada_Surf
  • 616
  • 1
  • 7
  • 19
  • 1
    @rekire I'd agree in theory, but in practice that doesn't really lead to an answer. – deceze Aug 29 '12 at 15:59
  • 3
    This one's better: [How can I encode a filename in PHP according to RFC 2231?](http://stackoverflow.com/questions/4968272/how-can-i-encode-a-filename-in-php-according-to-rfc-2231) – deceze Aug 29 '12 at 16:00
  • I am looking to decode a RFC2231 string, rather than encode into this format. – Nada_Surf Aug 29 '12 at 16:00
  • @deceze I was just adding that link – rekire Aug 29 '12 at 16:00
  • 1
    @deceze - The duplicates that are referenced are for _encoding_ rather than _decoding_. I am looking do decode. – Nada_Surf Aug 29 '12 at 16:11
  • What exactly is your question? You already know about RFC 2231, and it explains the format of that encoding, so all you have to do is follow that RFC. If you're asking for recommendations for existing libraries for parsing RFC2231-encoded strings, you're asking on the wrong site. – lanzz Oct 29 '12 at 19:47

2 Answers2

1

The akelos Framework does seem to have a decoding function you might look into it:

Akelos

Dukeatcoding
  • 1,363
  • 2
  • 20
  • 34
1

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.

HenchHacker
  • 1,616
  • 1
  • 10
  • 16