dacastro4 / laravel-gmail

Laravel wrapper for the Gmail API
MIT License
289 stars 130 forks source link

Dacastro4\LaravelGmail\Services\Message\Mail::__construct(): Argument #1 ($message) must be of type ?Google_Service_Gmail_Message, Google\Service\Exception given #275

Open stefanofinetti opened 5 months ago

stefanofinetti commented 5 months ago

Starting this week this package stopped working all of a sudden, producing this error message.

The complete error is: Dacastro4\LaravelGmail\Services\Message\Mail::__construct(): Argument #1 ($message) must be of type ?Google_Service_Gmail_Message, Google\Service\Exception given, called in /var/www/istitutiraggruppati/mercury/vendor/dacastro4/laravel-gmail/src/Services/Message.php on line 149

It seems it get an exception from Google about "too many request" which is impossible: they have ONE page where they use the APIs to connect to google mailbox and read the incoming email. There is no automated or scheduled connection to gmail whatsoever in the entire site, just that page. I tried to renews the oauth key in google console, I also tried to delete json credential files for users and have them authenticate again, but no luck.

stefanofinetti commented 5 months ago

I've temporary solved the problem by disabling the preload function. It seems that Google consider as too many requests when you try to preload a whole inbox. This is unexpected behavior from google anyway.

umarzahid028 commented 4 months ago

Hi @stefanofinetti I've been using this package for the last two years without any problems. However, today, I encountered the same error suddenly, and I'm unsure how to resolve it. When I remove the preload method, I get a different error. But if I use the take(50) method, it works, although it only returns 50 records. If you discover a proper solution, please inform me as my project is currently live.

Here's the code snippet: $messages = LaravelGmail::message()->preload()->take(50)->after($last_email_sync_date)->all();

stefanofinetti commented 4 months ago

Hi @umarzahid028 , this is the snippet of code I'm using right now in a live production environment. We use multiple accounts so you'll find the user auth:

try { $gmailUser = LaravelGmail::setUserId(Auth::user()->email);

        if ($gmailUser->check()) {

            $messages = $gmailUser->message()->in('INBOX')->all();
            $messaggi = collect($messages);

        } else {
            $messaggi = collect([]);
        }

        $msgPage = new LengthAwarePaginator($messaggi->forPage($request->page, 30), $messaggi->count(), 30, $request->page);
        $msgPage->withPath('/protocollo/posta');

        return view('protocollo.posta', [
            'messaggi' => $msgPage,
        ]);
    } catch (Exception $error) {
        Log::error('Errore accedendo a GMAIL', ['Data' => $error->getMessage()]);
        dd($error);
    }

The actual load of message is done after, in the view, in a foreach loop of all messages and for each call to $message i had to add ->load() before any other command:

$messaggio->load()->getLabels() $messaggio->load()->->getFromName()

and so on.

This is just a workaround untile preload() will work again (hopefully)

umarzahid028 commented 4 months ago

Thank you for your reply.

When I use message()->in('INBOX')->all();, I encounter a "Call to a member function flatten() on null" error.

if ($message && $message->hasAttachments()) { // handle attachments }

stefanofinetti commented 4 months ago

That, i think, it's because you should use $message->load() && $message->load()->hasAttachments()) { // handle attachments }

Without ->load() you stil do not have the message object fully loaded to be used.

umarzahid028 commented 4 months ago

Thank you very much! ->load() method is also working for me. Once again, I appreciate you saving my time.