mardi 4 juin 2019

How to retrieve email from multiple accounts using GmailAPI?

I have saved access token to DB and loop each users to get mail. it works fine for first user, for second user accesstoken is updated but it fetches mail of first user. I'm using LaravelGmail plugin

If i access with single user its working fine

public function sync()
{
    $stafflist = Staff::all();
    if (!empty($stafflist)) {
        foreach ($stafflist as $key => $value) {
            $this->service = new LaravelGmail;
            $set_token     = $this->setactiveToken($value->staffToken, $value->staffEmail);
            try {
                $messages = $this->service::message()->take(5)->all();

                if (!empty($messages)) {
                    foreach ($messages as $loop) {
                        $detmsg   = $this->service::message()->get($loop->getId());
                        $dettoken = $this->service::getAccessToken();

                        $maildate = $detmsg->getInternalDate();
                        $sender   = $detmsg->getHeader('From');
                        preg_match('/<(.*)>/', $sender, $matches);

                        $sender_mail = isset($matches[1]) ? $matches[1] : $sender;
                        $result      = Smailer::firstOrCreate(['mailReceiver' => $value->staffUID, 'mailThreadID' => $loop->getId()],
                            ['mailSender' => $sender_mail, 'mailSubject'                      => $detmsg->getSubject(),
                                'mailContent' => $detmsg->getPlainTextBody(), 'mailHasAttachment' => $detmsg->hasAttachments(), 'mailReceivedDt' => $maildate]);
                    }
                }
                unset($this->service);
            } catch (Google_Service_Exception $e) {

            }

        }
    }
    return redirect('control/mails');
}

private function setactiveToken($stoken = '', $email = '')
{
    $this->service::setAccessToken($stoken);
    if ($this->service::isAccessTokenExpired()) {
        $this->service::fetchAccessTokenWithRefreshToken($this->service::getRefreshToken());
        $token          = $this->service::getAccessToken();
        $token['email'] = $email;
        $resp           = Staff::where('staffEmail', $email)
            ->update(['staffToken' => json_encode($token)]);

    }
}



from Newest questions tagged laravel-5 - Stack Overflow http://bit.ly/2IhFebr
via IFTTT

Aucun commentaire:

Enregistrer un commentaire