laravel-gmail icon indicating copy to clipboard operation
laravel-gmail copied to clipboard

Not able to send mail via queue job

Open ashish-negi opened this issue 4 years ago • 1 comments

I am not able to send the email via queue job. There are 2 conditions:

  • If GOOGLE_ALLOW_MULTIPLE_CREDENTIALS option is set false, Then gmail-json.json is used for the authentication and email was sent via queue job.
  • Now I have enabled the multiple credentials option by setting GOOGLE_ALLOW_MULTIPLE_CREDENTIALS to true. In this case file is created with user id suffix ie. gmail-json-1.json. If I try to send the email I got the following error

{ "error": { "code": 401, "message": "Request is missing required authentication credential. Expected OAuth 2 access token, login cookie or other valid authentication credential. See https://developers.google.com/identity/sign-in/web/devconsole-project.", "errors": [ { "message": "Login Required.", "domain": "global", "reason": "required", "location": "Authorization", "locationType": "header" } ], "status": "UNAUTHENTICATED" } }

This is because LaravelGmail was not able to use the JSON file to authenticate. Is there a way to set the access token on the queue job? I checked the documentation and found that I can set access token by using $mail->using( $token ), but for this either I need to set the token as param in queue job or find the file with user id suffix.

Is there any inbuilt function to get the token by user id ?

ashish-negi avatar Aug 20 '21 14:08 ashish-negi

Inside of handle method of job Auth::user() not accessible. that's why gmail-json-{$authId}.json not found. You can access the LaravelGmail instance by following in handle method

     $gm = new LaravelGmailClass(config(), $this->userId);  //$userId is the authuser Id passed in the constructor of the job
     //for all mails
     $mails = $gm->message()->preload()->all();

skn-036 avatar Sep 12 '21 07:09 skn-036