Decode a Quoted Printable Message in PHP

decode a quoted printable message in php

This should help: quoted_printable_decode

PHP Invalid quoted-printable sequence, malformed q encoding from Yahoo

I created a solution that uses Roundcube's source code to decode the message.

I posted the code and demo:

  • You can see it here
  • Click the big play button to preview the extraction
  • Go to code tab to see the extracted Roundcube code that you could use for your project

Since you mentioned to not use classes in the example I extracted Roundcube's decode_mime_string() function from rube_mime, and a couple of things from rcube_charset such as $aliases, parse_charset(), and convert().


As far as decoding the malformed text from Yahoo:

=?UTF-8?Q?GOG_Forward =?UTF-8?Q?ck-off_with_Weekly_Sale_up_to_90%_off?=

Into this:

GOG Forward: Fw: A great Monday kick-off with Weekly Sale up to 90% off

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?

mime decode pdf quoted-printable

fixed by using quoted_printable_decode() on each line rather than the whole mime-part

Encode/Decode Original mail body to readable foreign (Chinese) character

It's a hexdecimal representation of their unicode values (E4B8AD, etc)

$string = "=E4=B8=AD=E4=B8=AD=E5=AD=97 smile";

$string = preg_replace_callback(
"/=([a-zA-Z0-9]{2})/",
function ($match) {
return hex2bin($match[1]);
},
$string
);

var_dump($string);

will result in string(15) "中中字 smile"

How to decode a quoted printable e-mail header (with MimeKit)

When you parse a message with MimeMessage.Load(), you don't need to decode headers because MimeKit will do it for you.

Secondly, your example header is not encoded using quoted-printable, it's encoded using rfc2047 tokens which would need to be decoded using Rfc2047.DecodeText():

var decoded = Rfc2047.DecodeText (Encoding.ASCII.GetBytes ("Hello =?UTF-8?B?TmFtZSDDpMO2w7w=?= more words"));

Converting Email ISO-8859-1 to UTF-8

I already found how to do this. Code:

$message='isto=2C isto =E9 um t=E9ste! :D';
$message=quoted_printable_decode($message)
$message=iconv('iso-8859-1','utf-8', $message);

Output:

isto, isto é um téste! :D


Related Topics



Leave a reply



Submit