I am syncing the user's email according to last history_id and its working fine. But sometimes it doesn't work and showing a message not found error. I have checked and found, It was syncing messages which get removed from INBOX or Bounced.
I have added one more parameter in $opt_pram array 'historyTypes'=>'messageAdded'
but still it's getting all the messages with deleted messages.
I have also tried with labelIds= INBOX but it's not working.
$opt_param = array('historyTypes'=>'messageAdded','startHistoryId' => $history_id,'maxResults'=>100);
$pageToken = NULL;
$histories = array();
do {
try {
if ($pageToken) {
$opt_param['pageToken'] = $pageToken;
}
$historyResponse = $service->users_history->listUsersHistory($user, $opt_param);
if ($historyResponse->getHistory()) {
$histories = array_merge($histories, $historyResponse->getHistory());
$pageToken = $historyResponse->getNextPageToken();
}
} catch (Exception $e) {
print 'An error occurred: ' . $e->getMessage();
}
} while ($pageToken);
In $histories
, it's containing deleted messages as well.
I am looking for a solution where I can get all valid/existing messages.
'historyTypes'=>'messageAdded'
will show you all messages added since the creation of the corresponding startHistoryId
, even if those messages have been preposterously deleted.As a workaround, I suggest you to make a second query for 'historyTypes'=>'messageDeleted'
and then compare the message Ids contained in the respective query responses and only sync the messages contained in the 'historyTypes'=>'messageAdded'
query, but not in the 'historyTypes'=>'messageDeleted'
query.