dacastro4 / laravel-gmail

Laravel wrapper for the Gmail API
MIT License
292 stars 134 forks source link

How to receive 5 most recent emails #148

Closed jstam89 closed 4 years ago

jstam89 commented 4 years ago

I recently started with this and now I would like some help to find out how i can receive the latest receiven emails read and unread. anyone willing to help a poor ol boi?

blankRSD commented 4 years ago

You can use the raw method to use filters. For example: For getting all unread messages in inbox $mails = LaravelGmail::message()->raw('in:inbox is:unread')->preload()->all(); For getting all read messages in inbox $mails = LaravelGmail::message()->raw('in:inbox is:read')->preload()->all();

You can test out the correct filters by using them in the Gmail search box. For more info: Link

jstam89 commented 4 years ago

It does get my unread emails but I'm getting an empty object as subject and body.

blankRSD commented 4 years ago

Did you use the preload() function? Preload fetches all the content of mail as mentioned in the README.

jstam89 commented 4 years ago

I did as you said. I'm getting a payload with sometimes 60 objects.. I've clicked trough 1 and found a subject somewhere.. but the actual data is not presenting a subject and body.

blankRSD commented 4 years ago

If you want to reduce the number of mails you can use the take() function. Eg: $mails = LaravelGmail::message()->raw('in:inbox is:unread')->take(5)->preload()->all();

After this, you can run a foreach loop on $mails to get the content as mentioned here Eg:


foreach($mails as $mail){
    echo $mail->getSubject();
    echo $mail->getHtmlBody();
}