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

社会主义新天地 提交于 2019-11-27 07:22:56

问题


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?


回答1:


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




回答2:


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()



回答3:


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;


来源:https://stackoverflow.com/questions/2722451/how-do-i-decode-mail-header-strings-with-their-encoding-type-in-them-in-php

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!