How can I get all inbox messages using Gmail API with PHP? I can just get message ID, and nothing beyond that. I need to fetch Subject, Sender and Message Text.
$list = $gmail->users_messages->listUsersMessages('me',['maxResults' => 10000, 'q' => 'category:primary']);
$messageList = $list->getMessages();
$client->setUseBatch(true);
$batch = new Google_Http_Batch($client);
foreach($messageList as $mlist){
$batch->add($gmail->users_messages->get('me',$mlist->id, ['format' => 'raw']),$mlist->id);
}
$batchMessages = $batch->execute();
$inboxMessage = [];
foreach($batchMessages as $dMessage){
$messageId = $dMessage->id;
$messageSnippet = $dMessage->snippet;
$dcMessage = base64url_decode($dMessage->getRaw());
$params['include_bodies'] = true;
$params['decode_bodies'] = true;
$params['decode_headers'] = true;
$mimeDecode = new Mail_mimeDecode($dcMessage);
$mimeSubject = $mimeDecode->decode($params)->headers['subject'];
$inboxMessage[] = [
'messageId' => $messageId,
'messageSubject' => $messageSubject
];
}
Let’s overview the steps to make your PHP app send emails with Gmail API. Go to Google Developers Console. Choose “Select a project” and make a new one. Name it and hit the “Create” button. On the left bar, choose “Library” and move to the API Library page. Find Gmail API and click on it.
To Get Emails From Gmail It Takes Only Two Steps:- Make a HTML file and define markup Make a PHP file to get emails from gmail
Enable IMAP library extension in the PHP configuration file removing the semicolon (;) at the beginning of the line. Restart apache to make these changes effective. Open Gmail and go to Settings -> Forwarding and POP/IMAP and enable IMAP access.
Note: For this quickstart, you are enabling the "Gmail API". A Google account with Gmail enabled. See the library's installation page for the alternative installation options. Create a file named quickstart.php in your working directory and copy in the following code: throw new Exception('This application must be run on the command line.');
This is how i did it.
$list = $gmail->users_messages->listUsersMessages('me', [
'maxResults' => 10,
'q' => $search
]);
$messageList = $list->getMessages();
$inboxMessage = [];
foreach($messageList as $mlist){
$optParamsGet2['format'] = 'full';
$single_message = $gmail->users_messages->get('me', $mlist->id, $optParamsGet2);
$message_id = $mlist->id;
$headers = $single_message->getPayload()->getHeaders();
$snippet = $single_message->getSnippet();
foreach($headers as $single) {
if ($single->getName() == 'Subject') {
$message_subject = $single->getValue();
} elseif ($single->getName() == 'Date') {
$message_date = $single->getValue();
$message_date = date('M jS Y h:i A', strtotime($message_date));
} elseif ($single->getName() == 'From') {
$message_sender = $single->getValue();
$message_sender = str_replace('"', '', $message_sender);
}
}
$inboxMessage[] = [
'messageId' => $message_id,
'messageSnippet' => $snippet,
'messageSubject' => $message_subject,
'messageDate' => $message_date,
'messageSender' => $message_sender
];
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With