Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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 this issue Mar 14, 2024 · 6 comments

Comments

@stefanofinetti
Copy link

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
Copy link
Author

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
Copy link

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
Copy link
Author

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
Copy link

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
Copy link
Author

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
Copy link

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants