开发者

How do I decode mail header strings with their encoding type in them in PHP

I'm creating a small, web based, mail client in PHP and noticed that a number of email Subjects and Contents appear 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 they display correctly?


This is an RFC 2047 encoded-word. It is decoded by the mb_decode_mimeheader function.


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

Mail_mimeDecode::_decodeHeader()


This is an old question but recently I came across this issue while parsing the emails. When printing the header info using function imap_header_info, 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 subject was "field name - need at risk into the label (some content to)"

In order to fix this issue, the function imap_mime_header_decode has to be used within 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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜