使用Gmail API从PHP中读取来自Gmail的邮件 [英] Reading messages from Gmail, in PHP, using Gmail API
本文介绍了使用Gmail API从PHP中读取来自Gmail的邮件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我已经从Google PHP客户端库中下载了新的Gmail API源代码.
I have donwloaded new Gmail API source code from Google PHP client library.
我使用以下方法初始化了服务
I inititalized the service using:
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邮件?
What should I do next? How to read Gmail messages using Gmail API PHP library?
推荐答案
为演示起见,您可以执行以下操作:
For the sake of demonstration, you can do something like this:
$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从PHP中读取来自Gmail的邮件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文