我正在尝试获取给定搜索的电子邮件数量(单独阅读和未读)。我已经读过labels.get()函数可以解决问题,但我不知道如何使用它。这是我必须检测的代码,如果我对给定的发件人有少于或多于100的结果。
require_once '../../vendor/autoload.php';
session_start();
$client = new Google_Client();
$client->setAuthConfigFile('../../client_secrets.json');
$client->addScope(Google_Service_Drive::DRIVE_METADATA_READONLY);
$client->addScope(Google_Service_Gmail::GMAIL_READONLY);
$client->setAccessType("offline");
$client->setApprovalPrompt('force');
$client->setAccessToken($_SESSION['token']);
$service = new Google_Service_Gmail($client);
$sender = array();
$sender[] = 'sender1@email.com';
$sender[] = 'sender2@email.com';
$sender[] = 'sender3@email.com';
function countfrom($service, $userId, $expeditor) {
try
{
unset($optParamsamz);
$optParamsamz = [];
$optParamsamz['maxResults'] = 100; // Return Only 5 Messages
$optParamsamz['q'] = "From: '".$expeditor."' ";
$messagesamz = $service->users_messages->listUsersMessages('me',$optParamsamz);
$listamz = $messagesamz->getMessages();
echo sizeof($listamz);
}
catch (Exception $e)
{
print 'An error occurred: ' . $e->getMessage();
}
}
foreach ($sender as $key => $value)
{
echo $value .': ';
countfrom($service,$_SESSION['emaile'],$value) ;
echo '<br/>';
}
-------------------编辑----------------------
我尝试了一种似乎更接近我正在寻找的新解决方案。现在的问题来自Google,它为结果评估返回了一些奇数:
<?
require_once '../../vendor/autoload.php';
session_start();
$client = new Google_Client();
$client->setAuthConfigFile('../../client_secrets.json');
$client->addScope(Google_Service_Drive::DRIVE_METADATA_READONLY);
$client->addScope(Google_Service_Gmail::GMAIL_READONLY);
$client->setAccessType("offline");
$client->setApprovalPrompt('force');
$client->setAccessToken($_SESSION['token']);
$service = new Google_Service_Gmail($client);
$sender_array[] = 'sender1@sender.com';
$sender_array[] = 'sender2@sender.com';
$sender_array[] = 'sender3@sender.com';
$sender_array[] = 'sender4@sender.com';
foreach ($sender_array as $key => $expeditor)
{
$optParamsamz1['q'] = "From: '".$expeditor."' is:read ";
$optParamsamz2['q'] = "From: '".$expeditor."' ";
echo $expeditor.": ".$service->users_messages->listUsersMessages('me',$optParamsamz1)->getResultSizeEstimate() . "
".$service->users_messages->listUsersMessages('me',$optParamsamz2)->getResultSizeEstimate();
echo "<br>";
}
&GT;
答案 0 :(得分:1)
labels.get()
在这个用例中没有任何帮助。它仅适用于标签,因此您可以从例如标签读取/未读取INBOX
或CHAT
很容易,但是如果您希望从中获取所有已读取/未读取内容,则无济于事从example@gmail.com
发送的所有邮件。
另一种解决方案相当便宜:
AND is:unread
列出邮件,使用相同的查询+列出第二个邮件
AND -is:unread
。nextPageToken
,则您有100多个已读/未读。如果它不包含nextPageToken
,则有response.messages.length
条已读/未读邮件。示例强>
请求未读
q = from:info@berniesanders.com AND is:unread
GET https://www.googleapis.com/gmail/v1/users/me/messages?q=from%3Ainfo%40berniesanders.com+AND+is%3Aunread&access_token={YOUR_API_KEY}
<强>响应强>
{
"messages": [
{
"id": "1523144d6e3feb2e",
"threadId": "1523144d6e3feb2e"
},
{
"id": "15227d879ccb601f",
"threadId": "15227d879ccb601f"
}, ...
}
// No nextPageToken => response.messages.length unread = 22 unread
请求未读
q = from:info@berniesanders.com AND -is:unread
GET https://www.googleapis.com/gmail/v1/users/me/messages?q=from%3Ainfo%40berniesanders.com+AND+-is%3Aunread&access_token={YOUR_API_KEY}
<强>响应强>
{
"messages": [
{
"id": "1522d4af39d7eec6",
"threadId": "1522d4af39d7eec6"
},
{
"id": "1521d6f3dbeaf886",
"threadId": "1521d6f3dbeaf886"
}, ...
"nextPageToken": "32436546446"
}
// nextPageToken in response => 100+ read
您可以更进一步,继续使用nextPageToken
列出,直到响应中没有nextPageToken
,然后将所有结果添加到一起,但这可能会导致速度变慢或效率低下为您的用例。