I'm trying to read the email body usign Gmail API
I was using IMAP but for performance reasons(reading emails takes so much time in IMAP) I have to move to Gmail API which is considerably faster.
The issue is where I'm trying to decode the Body, with IMAP is simple, just read the transfer encodings from imap_fetchstructure return and use the appropriate function to decode. (imap_qprint,imap_7bit, etc)
And for Gmail
$message = $service->users_messages->get($user, $msg->id, ["format"=>"full"]);
$payload = $message->getPayload();
$mime = $payload->getMimeType();
$body = $payload->getBody();
$headers = $payload->getHeaders();
$content = $body->getData();
$decoded = base64_decode($content);
The variable $contents is the body in base64 but If I decode includes strange characters like ��ѽ��� that didn't happened with IMAP. The content is plain text UTF-8 no additional parts or attachment, just plain text. And it happens with HTML as well.
These are the relevants headers
[{"name":"MIME-Version","value":"1.0"},
{"name":"Content-Type","value":"text\/plain; charset=utf-8"},
{"name":"Content-Transfer-Encoding","value":"quoted-printable"}]
I think the issue is that the body is quoted-printable but even if I use imap_qprint or quoted_printable_decode over the decoded base64 these strange characters continue.
I was having the same problem...Felipe Morales got at the solution...but to be a bit more explicit:
Take the base64-url-encoded string from the API response, and run it through this function:
function gmailBodyDecode($data) {
$data = base64_decode(str_replace(array('-', '_'), array('+', '/'), $data));
//from php.net/manual/es/function.base64-decode.php#118244
$data = imap_qprint($data);
return($data);
}
Works like a champ...
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With