I came across the following error in PHP generated by an email forwarded from a Yahoo account:
Notice: Unknown: Invalid quoted-printable sequence: =?UTF-8?Q?ck-off with Weekly Sale up to 90% off (errflg=3) in Unknown on line 0
I’ve spent hours researching this issue and decided to send myself the exact same output string in an email without having Yahoo involved. The original q-encoded text that decodes correctly:
=?UTF-8?Q?GOG_Forward=3A_Fw=3A_=F0=9F=98=89_A_great_Monday_kick-?= =?UTF-8?Q?off_with_Weekly_Sale_up_to_90=25_off?=
The malformed q-encoded text from Yahoo:
=?UTF-8?Q?GOG_Forward =?UTF-8?Q?ck-off_with_Weekly_Sale_up_to_90%_off?=
The correct string when decoded:
GOG Forward: Fw: 😉 A great Monday kick-off with Weekly Sale up to 90% off
Roundcube manages to decode both the normal and the malformed text though I’m not sure how and 25 megabytes is a bit much to dig through and I haven’t been able to determine even where they’re decoding subject headers.
How do I fix Yahoo’s malformed version of q-encoding?
<?php
//These fail:
echo imap_mime_header_decode($mail_message_headers['Subject']);
echo quoted_printable_decode($mail_message_headers['Subject']);
?>
For clarification the imap_fetchstructure page clarifies the value 4
for encoding is Quoted-Printable
/ ENCQUOTEDPRINTABLE
.
New Development
It turns out that for some reason Yahoo sends the subject twice for the same header, one malformed and the other is not. Here is the Subject
header from the raw email:
Subject: =?UTF-8?Q?GOG_Forward:_Fw:_=F0=9F=98=89_A_great_Monday_ki?=
=?UTF-8?Q?ck-off_with_Weekly_Sale_up_to_90%_off?=
MIME-Version: 1.0
2
Answers
I created a solution that uses Roundcube’s source code to decode the message.
I posted the code and demo:
Since you mentioned to not use classes in the example I extracted Roundcube’s
decode_mime_string()
function fromrube_mime
, and a couple of things fromrcube_charset
such as$aliases
,parse_charset()
, andconvert()
.As far as decoding the malformed text from Yahoo:
Into this:
It’s impossible. There’s not enough data in there. For example it’s missing the "😉 A great Monday ki". Do you have the full source of the email address?
You don’t actually need to go for any third party solution. There is already an in-built imap function for decoding strings like the one you given, i.e.
imap_utf8
and works pretty fine. Here’s an example taken from your question.And as for your doubt why it was throwing that error for
quoted_printable_decode
, well actually your string is of typeutf8
encoded.