【问题标题】:Decoding html email body Gmail api解码 html 电子邮件正文 Gmail api
【发布时间】:2018-07-28 22:39:44
【问题描述】:

使用 Gmail api 时解码 html 电子邮件正文的正确方法是什么。

// Expected: "<p><strong>Test test</strong></p>"
$message = $gmail->users_messages->get('me', $messageId);
$payload = $message->getPayload();
$body = $payload->getBody()->data;
Log::info('Raw body:', [$body]); // "PHA-PHN0cm9uZz5UZXN0IHRlc3Q8L3N0cm9uZz48L3A-"
Log::info('Base64 decoded body:', [base64_decode($body)]); // NO OUTPUT
Log::info('UTF8 encoded body:', [utf8_encode($body)]); // "PHA-PHN0cm9uZz5UZXN0IHRlc3Q8L3N0cm9uZz48L3A-"
Log::info('Quote print decoded:', [quoted_printable_decode($body)]); // "PHA-PHN0cm9uZz5UZXN0IHRlc3Q8L3N0cm9uZz48L3A-"
Log::info('Quote print decoded:', [quoted_printable_decode(base64_decode($body)))]); // NO OUTPUT

【问题讨论】:

    标签: php laravel decode gmail-api


    【解决方案1】:

    想通了。它需要一些额外的技巧,特别是 base64url_decode 函数found here

    protected function base64url_decode($data) { 
            return base64_decode(str_pad(strtr($data, '-_', '+/'), strlen($data) % 4, '=', STR_PAD_RIGHT)); 
    
    }
    
    public function getMessage($messageId) {
        // ...
        $message = $gmail->users_messages->get('me', $messageId);
        $payload = $message->getPayload();
        $body = $payload->getBody()->data;
        // decoded body
        return $decodedBody= quoted_printable_decode($this->base64url_decode($rawBody));
    }
    

    【讨论】:

    • 您是否知道任何可以实际过滤电子邮件正文内容的脚本,例如电子邮件中收到的收据。该脚本应该能够过滤掉商品名称、价格、购买日期、税金等。
    • @user9465677 听起来您需要特定于您的用例的东西。
    猜你喜欢
    • 2017-10-28
    • 2015-09-25
    • 2018-03-03
    • 2015-06-15
    • 2017-03-07
    • 2017-11-01
    • 2014-08-17
    • 2015-01-02
    • 2014-05-15
    相关资源
    最近更新 更多