How to decode message header lines with their encoding type in them in PHP

I am creating a small, web-based email client in PHP and noticed that several email messages and content are displayed as follows:

=?ISO-8859-1?Q?Everything_for_=A35_-_Box_Sets,_Games_?= =?ISO-8859-1?Q?and_CD_Soundtracks...hurry,_ends_soon?=
=?utf-8?B?UGxheS5jb206IE9uZSBEYXkgT25seSDigJMgT3V0IG9mIHRoaXMgV29ybGQgRGVhbHMh?=
=?windows-1252?Q?Jon,_delivery_on_us_&_earn_=A35_credit_or_50_prints?=

Does anyone have any ideas for decoding them so that they display correctly?

+3
source share
3 answers

This is RFC 2047 coded word. It is decoded by function mb_decode_mimeheader.

+4
source

This is a MIME encoded string, mainly used for headers. You can find many libraries that can handle this. For example, get PEAR :: mail and use this function,

Mail_mimeDecode::_decodeHeader()
+2
source

This is an old question, but I recently ran into this problem while parsing emails. When printing header information using the imap_header_info function , the following array was shown:

stdClass Object
(
    [subject] => =?Windows-1252?Q?field_name_-_need___`at_risk=92____into_t?= =?Windows-1252?Q?he_label_(_some_content_to_)_?=
)

However, the original object was " field name - you need to take a chance in the label (some content) "

To fix this problem, the imap_mime_header_decode function should be used in a loop to generate the correct text:

$header = imap_headerinfo($email_obj, $email_ref_number, 0);
$elements = imap_mime_header_decode($header->subject);
$email_subject = '';
if ( ! empty($elements)) {
    foreach ($elements AS $e_part) {
        if (isset($e_part->text)) {
            $email_subject .= $e_part->text;
        }
    }
}
echo $email_subject;
0
source

All Articles