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 8931985
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T09:14:20+00:00 2026-06-15T09:14:20+00:00

I’m trying to pipe my incoming mails to a PHP script so I can

  • 0

I’m trying to pipe my incoming mails to a PHP script so I can store them in a database and other things. I’m using the class MIME E-mail message parser (registration required) although I don’t think that’s important.

I have a problem with email subjects. It works fine when the title is in English but if the subject uses non-latin Characters I get something like

=?UTF-8?B?2KLYstmF2KfbjNi0?=

for a title like
یک دو سه

I decode the subject like this:

  $subject  = str_replace('=?UTF-8?B?' , '' , $subject);
  $subject  = str_replace('?=' , '' , $subject);      
  $subject = base64_decode($subject); 

It works fine with short subjects with like 10-15 characters but with a longer title I get half of the original title with something like ��� at the end.

If the title is even longer, like 30 characters, I get nothing. Am I doing this right?

  • 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-06-15T09:14:21+00:00Added an answer on June 15, 2026 at 9:14 am

    Despite the fact that this is almost a year old – I found this and am facing a similar problem.

    I’m unsure why you’re getting odd characters, but perhaps you are trying to display them somewhere your charset is unsupported.

    Here’s some code I wrote which should handle everything except the charset conversion, which is a large problem that many libraries handle much better. (PHP’s MB library, for instance)

    class mail {
        /**
          * If you change one of these, please check the other for fixes as well
         *
         * @const Pattern to match RFC 2047 charset encodings in mail headers
         */
        const rfc2047header = '/=\?([^ ?]+)\?([BQbq])\?([^ ?]+)\?=/';
    
        const rfc2047header_spaces = '/(=\?[^ ?]+\?[BQbq]\?[^ ?]+\?=)\s+(=\?[^ ?]+\?[BQbq]\?[^ ?]+\?=)/';
    
        /**
         * http://www.rfc-archive.org/getrfc.php?rfc=2047
         *
         * =?<charset>?<encoding>?<data>?=
         *
         * @param string $header
         */
        public static function is_encoded_header($header) {
            // e.g. =?utf-8?q?Re=3a=20Support=3a=204D09EE9A=20=2d=20Re=3a=20Support=3a=204D078032=20=2d=20Wordpress=20Plugin?=
            // e.g. =?utf-8?q?Wordpress=20Plugin?=
            return preg_match(self::rfc2047header, $header) !== 0;
        }
    
        public static function header_charsets($header) {
            $matches = null;
            if (!preg_match_all(self::rfc2047header, $header, $matches, PREG_PATTERN_ORDER)) {
                return array();
            }
            return array_map('strtoupper', $matches[1]);
        }
    
        public static function decode_header($header) {
            $matches = null;
    
            /* Repair instances where two encodings are together and separated by a space (strip the spaces) */
            $header = preg_replace(self::rfc2047header_spaces, "$1$2", $header);
    
            /* Now see if any encodings exist and match them */
            if (!preg_match_all(self::rfc2047header, $header, $matches, PREG_SET_ORDER)) {
                return $header;
            }
            foreach ($matches as $header_match) {
                list($match, $charset, $encoding, $data) = $header_match;
                $encoding = strtoupper($encoding);
                switch ($encoding) {
                    case 'B':
                        $data = base64_decode($data);
                        break;
                    case 'Q':
                        $data = quoted_printable_decode(str_replace("_", " ", $data));
                        break;
                    default:
                        throw new Exception("preg_match_all is busted: didn't find B or Q in encoding $header");
                }
                // This part needs to handle every charset
                switch (strtoupper($charset)) {
                    case "UTF-8":
                        break;
                    default:
                        /* Here's where you should handle other character sets! */
                        throw new Exception("Unknown charset in header - time to write some code.");
                }
                $header = str_replace($match, $data, $header);
            }
            return $header;
        }
    }
    

    When run through a script and displayed in a browser using UTF-8, the result is:

    آزمایش

    You would run it like so:

    $decoded = mail::decode_header("=?UTF-8?B?2KLYstmF2KfbjNi0?=");
    
    • 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
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and
I'm trying to convert HTML to plain text. I get many &\#8217; &\#8220; etc.
I'm trying to create an if statement in PHP that prevents a single post
Let's say I'm outputting a post title and in our database, it's Hello Y&#8217;all
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has

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.