gpt4 book ai didi

gmail-api - 使用Gmail API从PHP中读取来自Gmail的邮件

转载 作者:行者123 更新时间:2023-12-03 13:50:27 25 4
gpt4 key购买 nike

我已经从Google PHP客户端库中下载了新的Gmail API源代码。

我使用以下方法初始化了该服务:

set_include_path("./google-api-php-client-master/src/".PATH_SEPARATOR.get_include_path());

require_once 'Google/Client.php';
require_once 'Google/Service/Gmail.php';

$client = new Google_Client();
$client->setClientId($this->config->item('gmailapi_clientid'));
$client->setClientSecret($this->config->item('gmailapi_clientsecret'));
$client->setRedirectUri(base_url('auth'));
$client->addScope('email');
//$client->addScope('profile');
$client->addScope('https://mail.google.com');
$client->setAccessType('offline');

$gmailService = new Google_Service_Gmail($client);


接下来我该怎么办?如何使用Gmail API PHP库阅读Gmail邮件?

最佳答案

为了演示起见,您可以执行以下操作:

        $optParams = [];
$optParams['maxResults'] = 5; // Return Only 5 Messages
$optParams['labelIds'] = 'INBOX'; // Only show messages in Inbox
$messages = $service->users_messages->listUsersMessages('me',$optParams);
$list = $messages->getMessages();
$messageId = $list[0]->getId(); // Grab first Message


$optParamsGet = [];
$optParamsGet['format'] = 'full'; // Display message in payload
$message = $service->users_messages->get('me',$messageId,$optParamsGet);
$messagePayload = $message->getPayload();
$headers = $message->getPayload()->getHeaders();
$parts = $message->getPayload()->getParts();

$body = $parts[0]['body'];
$rawData = $body->data;
$sanitizedData = strtr($rawData,'-_', '+/');
$decodedMessage = base64_decode($sanitizedData);

var_dump($decodedMessage);

关于gmail-api - 使用Gmail API从PHP中读取来自Gmail的邮件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24503483/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com