logikgp's avatar

Decoding the message body from the gmail api?

Hello everyone, I successfully got everything to work.. however one problem remains. The body of the message, which I base64_decode()... the html has a lot of weird and unusual characters. Is there a way to clean this up? or should I return the base64 encoded string via json to javascript/angular and decode it there?

Example of unwanted characters:

BˆÝƒBˆÝƒBÝX›@РУƖÖrv–GFƒÒ#‚"†V–v‡CÒ#‚"ÇCÒ""7&3Ò

Thanks in advance!

0 likes
1 reply
sanderlissenburg's avatar

Sorry for answering this question 8 years later, but for any one interested...

The Gmail API often uses a variant of base64 encoding called "URL-safe base64" where '+' is replaced with '-', and '/' is replaced with '_'.

$base64String = $response->json()['payload']['parts'][1]['body']['data'] ?? '';

$base64String = strtr($base64String, '-_', '+/');

$decoded = base64_decode($base64String);

Please or to participate in this conversation.