【问题标题】:How to decode a base64 email with php?如何使用 php 解码 base64 电子邮件?
【发布时间】:2020-04-29 08:36:16
【问题描述】:

我正在尝试使用 base 64 解码方法来解码我的消息。有谁知道如何做到这一点,或者通过 php 函数?

<?php

class Gmail
{
    public function __construct($client)
    {
        $this->client = $client;
    }
    public function readLabels()
    {
        $service = new Google_Service_Gmail($this->client);

        // Print the labels in the user's account.
        $user = 'me';
        $results = $service->users_labels->listUsersLabels($user);


        $the_html = "";
        if (count($results->getLabels()) == 0) {
            // print "No labels found.\n";
            $the_html .= "<p>No labels found</p>";
        } else {
            // print "Labels:\n";
            $the_html .= "<p>labels</p>";


            foreach ($results->getLabels() as $label) {
                // printf("- %s\n", $label->getName());
                $the_html .= "<p>" . $label->getName() . "</p>";
            }
            return $the_html;
        }
    }
    /**
     * Get list of Messages in user's mailbox.
     *
     * @param  Google_Service_Gmail $service Authorized Gmail API instance.
     * @param  string $userId User's email address. The special value 'me'
     * can be used to indicate the authenticated user.
     * @return array Array of Messages.
     */
    public function listMessages()
    {
        $service = new Google_Service_Gmail($this->client);

        // Print the labels in the user's account.
        $userId = 'me';
        $pageToken = null;
        $messages = array();
        $opt_param = array();
        $messagesResponse = array();

        $i = 0;
        do {
            if ($i == 5) break;
            $i++;
            try {
                if ($pageToken) {
                    $opt_param['pageToken'] = $pageToken;
                }
                $messagesResponse = $service->users_messages->listUsersMessages($userId, $opt_param);
                if ($messagesResponse->getMessages()) {
                    $messages = array_merge($messages, $messagesResponse->getMessages());
                    $pageToken = $messagesResponse->getNextPageToken();
                }
            } catch (Exception $e) {
                print 'An error occurred: ' . $e->getMessage();
            }
        } while ($pageToken);

        foreach ($messages as $message) {
            print 'Message with ID: ' . $message->getId() . '<br/>';
            $msg = $service->users_messages->get($userId, $message->getId());
            echo "<pre>" . var_export($msg->payload->parts[1]->body->data->base64_decode, true) . "</pre>";
        }

        return $messages;
    }
}

【问题讨论】:

  • 你的意思是base64_decode
  • 如果你发布了几十行代码(这通常对我们很有帮助),那么你还应该说明代码应该做什么以及它在哪里失败。单击问题下方的edit 链接以添加更多详细信息,例如错误消息、示例输入、输出等。

标签: php base64 decode gmail-api


【解决方案1】:

正如@Marvin 上面评论中提到的,尝试使用base64_decode 函数。

这是一个例子:

$str = 'VGhpcyBpcyBhbiBlbmNvZGVkIHN0cmluZw==';
echo base64_decode($str); // This is an encoded string

所以在你的情况下,而不是使用

echo "<pre>" . var_export($msg->payload->parts[1]->body->data->base64_decode, true) . "</pre>";

试试

echo "<pre>" . var_export(base64_decode($msg->payload->parts[1]->body->data), true) . "</pre>";

参考

base64_decode

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-05-15
    • 2016-08-16
    • 2018-04-15
    • 2020-02-06
    • 1970-01-01
    • 2014-09-08
    • 2015-09-21
    • 2018-07-04
    相关资源
    最近更新 更多