I have configured everything and working perfectly, As a testing I'm trying to take some filtered emails and update them as read. Single update is working with below code
$mods = new Google_Service_Gmail_ModifyMessageRequest();
$mods->setRemoveLabelIds('UNREAD');
foreach ($messages as $message) {
try {
$message = $service->users_messages->modify($userId, $messageId, $mods);
print 'Message with ID: ' . $messageId . ' successfully modified.';
} catch (Exception $e) {
print 'An error occurred: ' . $e->getMessage();
}
print 'Message with ID: ' . $message->getId() . '<br/>';
}
I tried to refer the documentation to find out how to set the parameters to batchmodify function. but there is no details about that. so I read the source files in google client library and found it only accepts three parameters as below.
class Google_Service_Gmail_Resource_UsersMessages extends Google_Service_Resource
public function batchModify($userId, Google_Service_Gmail_BatchModifyMessagesRequest $postBody, $optParams = array())
So I tried below code
$mods = new Google_Service_Gmail_BatchModifyMessagesRequest();
$mods->setRemoveLabelIds(['UNREAD']);
$msgIdString = '';
foreach ($messages as $message) {
$id = $message->getId();
$msgIdString .= " $id";
}
echo $msgIdString;
try {
$message = $service->users_messages->batchModify($user, $mods);
} catch (Exception $e) {
print 'An error occurred: ' . $e->getMessage();
}
but this giving me an error
An error occurred: { "error": { "errors": [ { "domain": "global", "reason": "invalidArgument", "message": "No message ids specified." } ], "code": 400, "message": "No message ids specified." } }
So my problem is I still couldn't find the place that I should put the message ids.
After wasting some time on it, I could found it. I'm mentioning here as it will be help to someone in future, Actually the IDs should be an array instead of a string. that's mistake I have done before. So I push all the IDs in to $msgIdArray and sent. now it is working.
foreach ($messages as $message) {
$id = $message->getId();
$msgIdString .= " '$id', ";
array_push($msgIdArray, $id);
}
echo $msgIdString;
if (!empty($msgIdArray)) {
$mods = new Google_Service_Gmail_BatchModifyMessagesRequest();
$mods->setRemoveLabelIds(['UNREAD']);
$mods->setIds($msgIdArray);
try {
$message = $service->users_messages->batchModify($user, $mods);
} catch (Exception $e) {
print 'An error occurred: ' . $e->getMessage();
}
}