Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get all inbox messages with Gmail API using PHP

Tags:

php

gmail-api

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
        ];
    }
like image 442
Dejan Prole Avatar asked Feb 07 '15 22:02

Dejan Prole


People also ask

How to send emails with Gmail API in PHP?

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.

How to get emails from Gmail?

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

How to enable IMAP in Gmail using PHP?

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.

How do I create a Quickstart for the Gmail API?

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.');


1 Answers

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
    ];
}
like image 56
Dejan Prole Avatar answered Oct 19 '22 12:10

Dejan Prole