所以我正在尝试使用GMail API来调用收件箱中的电子邮件,以便我可以在自己的应用中使用它们。我已经对所有访问进行了排序,并且当前正在返回一个数组。我遇到的问题是尝试显示消息。
我的php看起来如下
<?php
ini_set('display_errors' , 'On');
error_reporting(E_ALL);
// Start Session
session_start();
//Include API autoloader
require_once __DIR__ . '/vendor/autoload.php';
// Create Google Client
$client = new Google_Client();
$client->setClientId('ID in here');
$client->setClientSecret('Secret here');
$client->setRedirectUri('http://localhost:8888/gmail/');
$client->addScope('https://mail.google.com/');
//Create Google Service
$service = new Google_Service_Gmail($client);
if(isset($_REQUEST['logout'])){
unset($_SESSION['access_token']);
}
// Check if there is an auth token
if(isset($_GET['code']))
{
$client->authenticate($_GET['code']);
$_SESSION['access_token'] = $client->getAccessToken();
$url = 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF'];
header('Location: ' . filter_var($url, FILTER_VALIDATE_URL));
}
// Check for access token in session
if(isset($_SESSION['access_token']))
{
$client->setAccessToken($_SESSION['access_token']);
} else
{
$loginUrl = $client->createAuthUrl();
echo 'Click <a href="' . $loginUrl . '">here</a> to login';
}
// Check if we have an access token ready for API Call
try
{
if(isset($_SESSION['access_token']) && $client->getAccessToken())
{
//make API calls
$messages = $service->users_messages->listUsersMessages('me',['maxResults'=> 2, 'labelIds'=> 'INBOX']);
foreach ($messages as $message)
{
print 'Message with ID: ' . $message->getId() . '<br/>';
}
return $messages;
}
}
catch (Google_Auth_Exception $e)
{
echo 'Looks like you dont have an access token or its expired';
}
答案 0 :(得分:0)
添加@Tholle建议的内容,messages
列表将包含具有From
属性的标头,以指示邮件的发件人。您只需在foreach messages
内添加新循环或专门调用payload[x].header.From