Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6612315
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T20:04:59+00:00 2026-05-25T20:04:59+00:00

I’m using Mail_mimeDecode to extract attachments from incoming emails. Everything was working well for

  • 0

I’m using Mail_mimeDecode to extract attachments from incoming emails. Everything was working well for a while, until I started receiving attachments with filenames encoded in KOI8, with a section header like this:

Content-Disposition: attachment; filename="=?KOI8-R?B?8NLJzM/Wxc7JxSAudHh0?="

mimeDecode does a perfectly reasonable thing and returns the filename in KOI8:

$attachmentNameInKOI8 = $part->d_parameters['filename'];

The problem is that I need it in UTF-8. In this specific example, I can run the following to do the conversion:

$attachmentNameInUTF8 = iconv('KOI8', 'UTF-8', $attachmentNameInKOI8);

But without trying to parse the message manually, I don’t know when the name is in KOI8 and when it’s not. I’m also worried that some other encoding will come through soon, so I need a way to handle anything that might come my way.

I had read that mb_detect_encoding is not reliable, and in fact I could not get it to detect the string as KOI8.

Is there a way to tell mimeDecode to do the translation for me? I looked at the sourcecode of mimeDecode.php:_decodeHeader() and I can see that it parses the encoding but then does nothing with it, which seems a wasted opportunity.

UPDATE: To be clear, this is only a problem with headers and not with bodies because mimeDecode exposes the charset of the body, so it’s very easy to run iconv yourself like this:

$bodyutf = iconv($textpart->ctype_parameters['charset'], 'UTF-8', $textpart->body);
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T20:05:00+00:00Added an answer on May 25, 2026 at 8:05 pm

    Adding a line to _decodeHeader() before the replace seems to do the trick:

    $text = iconv($charset, 'UTF-8', $text);
    $input = str_replace($encoded, $text, $input);
    

    Seems weird that they didn’t build some such option into the original class, doesn’t it?

    NOTE: I’ve since noticed that Subject lines and other headers can also be encoded the same way as filenames (RFC2047). It appears that adding the iconv line into _decodeHeader addresses all these cases.

    Weird that such a feature wasn’t already built into mimeDecode–this can’t be a rare problem.

    EDIT: I now understand that the point of mimeDecode having an option for decode_headers=false is to get the raw values so you can decode them yourself. This seems such a waste given that there’s no point to having mimeDecode decode your headers ever if you can’t trust that it’s going to return a string in an expected charset (it would make more sense for it to accept a charset as a parameter to decode to; or null means no decoding… I have a feeling they’re unlikely to change it for little me.) So the point is you need to do your own decoding. Unfortunately it’s not as simple as a straight call to imap_utf8() or imap_mime_header_decode(). You could either take the _decodeHeader() function from mimeDecode and modify it or use something like this:

    http://www.php.net/manual/en/function.imap-mime-header-decode.php#71762

    EDIT #2: Unbelievably, the mimeDecode guys already incorporated my suggestion into their latest svn:

    https://pear.php.net/bugs/bug.php?id=18876

    On that version, you can now set decode_headers=’UTF-8′ and mimeDecode will do all the work for you. Wow!

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm new to using the Perl treebuilder module for HTML parsing and can't figure
That's pretty much it. I'm using Nokogiri to scrape a web page what has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
For some reason, after submitting a string like this Jack’s Spindle from a text
I am reading a book about Javascript and jQuery and using one of the
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and
I am currently running into a problem where an element is coming back from
We're building an app, our first using Rails 3, and we're having to build
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.