简体   繁体   English

在php gmail api中解码邮件正文?

[英]Decode email body in php gmail api?

I'm trying to read the email body usign Gmail API 我正在尝试阅读电子邮件正文使用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. 我使用IMAP但出于性能原因(阅读电子邮件需要花费大量时间在IMAP上)我必须转移到Gmail API,这要快得多。

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. 问题是我正在尝试解码Body,IMAP很简单,只需从im​​ap_fetchstructure返回读取传输编码并使用相应的函数进行解码。 (imap_qprint,imap_7bit, etc) (imap_qprint,imap_7bit等)

And for Gmail 对于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. 变量$ contents是base64中的主体但是如果我解码包含像IM这样没有发生的奇怪字符。 The content is plain text UTF-8 no additional parts or attachment, just plain text. 内容是纯文本UTF-8没有附加部分或附件,只是纯文本。 And it happens with HTML as well. 它也发生在HTML上。

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. 我认为问题在于主体是引用可打印的,但即使我在解码的base64上使用imap_qprint或quoted_printable_decode,这些奇怪的字符仍在继续。

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: 从API响应中获取base64-url编码的字符串,并通过此函数运行它:

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... 像冠军一样工作......

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM