Skip to content
Advertisement

Using Gmail Api with Laravel PHP to read all gmail inbox on website and then respond to a particular email

I want to access my gmail inbox list in laravel php using Gmail API.I can do it on the console using PHP but it is difficult to find the sources to run the code on website using Laravel Framework.

Note that I don’t want to send the email to the recipient but wanted to fetch my whole inbox list of emails on my website. Had anyone face this issue and got any result?

Advertisement

Answer

If you want to retrieve your email inbox to then use these messages in your website you can combine the method users.messages.list() which will return a list of your email messages ids as described in the documentation along with users.messages.get() which will return the message itself.

In PHP these methods would be listUserMessages and get returning each a ListMessagesResponse and a GmailMessage. With them you can store the content of each message as an element of an array to then use this array in your website in your preferred way.

The following piece of code assumes that you have authorised your application correctly and only focuses on the logic to get an array of your inbox in PHP, it also has self explanatory comments:

// Run the method list which will return the list of messages 
$list = $gmail->users_messages->listUsersMessages('me');

// Get the actual list 
$messageList = $list->getMessages();

// Create array where we will store our messages
$messages = array();

// iterate over all the elements retrieved by the method list
foreach($messageList as $msg){

  // GET individual message
  $message = $gmail->users_messages->get('me',$msg->id);
  
  // Push the element into our array of messages
  array_push($messages,)
}
User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement