Not able to send mail via queue job
I am not able to send the email via queue job. There are 2 conditions:
- If GOOGLE_ALLOW_MULTIPLE_CREDENTIALS option is set
false, Thengmail-json.jsonis 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 ?
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();