4

I am pulling some code from an email (google) and it translates the code or encrypts it when I use an @ symbol it changes to and upside down explanation point.¡ I use the code below and it fixes it back to the @ symbol but it adds a "?" and the end or a "n". It also changes the $ to a funky symbol. Any ideas how to decode this properly? Again its a Google encryption. Don't know if that helps...

$Body = base64_decode($Body);
$Body = mb_convert_encoding($Body, "utf-8");
$Body = htmlspecialchars($Body);
$Body = preg_replace('/¡/',"@",$Body);
John Conde
  • 217,595
  • 99
  • 455
  • 496
Papa De Beau
  • 3,744
  • 18
  • 79
  • 137
  • What happens if you simply `base64_decode` it and output the value (no `mb_convert_encoding`, `htmlspecialchars` or `preg_replace`)? – deceze Dec 04 '11 at 07:16
  • I just tried that. Didn't work. I put in... "PDF @@@" and it came out as... Pdf ¡¡¡ n – Papa De Beau Dec 04 '11 at 07:20
  • 5
    How are you looking at the result? What encoding is the original in? It's an email? Look at its headers what encoding it's supposed to be. – deceze Dec 04 '11 at 07:23
  • Are you sure the input is base64-encoded? Could you quote a part of it and the MIME headers from the mail? – Anders Lindahl Dec 04 '11 at 08:06
  • Backtracking blind through Character Encoding lists is very time consuming. We need to know the character encoding from the email, however I feel you'll probably answer you're own question when you do find it. – David Barker Dec 04 '11 at 10:10
  • Here is the header section your asking for ----------------------------------------------- Content-Type: text/plain; charset=ISO-8859-1; format=flowed; delsp=yes Content-Transfer-Encoding: base64 UGRmIKGhoQ0KDQotLQ0KU2VudCB1c2luZyBTTVMtdG8tZW1haWwuICBSZXBseSB0byB0aGlzIGVt YWlsIHRvIHRleHQgdGhlIHNlbmRlciBiYWNrIGFuZCAgDQpzYXZlIG9uIFNNUyBmZWVzLg0KaHR0 cHM6Ly93d3cuZ29vZ2xlLmNvbS92b2ljZQ0K ---------------------------------------- This last part is the message I think. It comes out like this below... ----------------- – Papa De Beau Dec 04 '11 at 14:20
  • ---------------------- Pdf ¡¡¡ ---------------------- Then I use $Body = preg_replace('/¡/',"@",$Body); to change it to PDF @@@ but I still get funny characters in the end. You guys are great. Thanks for the effort. – Papa De Beau Dec 04 '11 at 14:21
  • INTERESTING NOTE: When I decode it before entering a MySQL database it gets those funky chars. If I use... $Body = preg_replace('/¡/',"@",$Body); after I pull from the database it works fine. I of course use the code below before I put it in a database--------------------$Body = base64_decode($Body); $Body = mb_convert_encoding($Body, "utf-8"); – Papa De Beau Dec 04 '11 at 14:35
  • Reposted as [Google encoding issue base64 and PHP decode](http://stackoverflow.com/questions/8379252/google-encoding-issue-base64-and-php-decode) – deceze Dec 04 '11 at 23:31

1 Answers1

1

I use this for fetching my email body from gmail.

$body = imap_fetchbody($inbox, $mail, 1);
$body = quoted_printable_decode($body);
$body = urldecode($body);

And it's work well

jmaitrehenry
  • 2,190
  • 21
  • 31
  • That won't work correctly for all messages; you have to look at the `Content-Transfer-Encoding` header of the message to know what to do to it. –  Dec 06 '11 at 14:56