From aa9970326cf0e83c9e785608c01d52f2bbf205d1 Mon Sep 17 00:00:00 2001 From: David Bomba Date: Wed, 17 Feb 2021 11:25:30 +1100 Subject: [PATCH] Fixes for multi mailer - gmail --- app/Helpers/Mail/GmailTransport.php | 10 +- app/Jobs/Mail/BaseMailerJob.php | 8 +- app/Jobs/Mail/NinjaMailerJob.php | 26 ++--- .../SendVerificationNotification.php | 1 + app/Mail/TemplateEmail.php | 2 +- config/gmail.php | 99 +++++++++++++++++++ 6 files changed, 128 insertions(+), 18 deletions(-) create mode 100644 config/gmail.php diff --git a/app/Helpers/Mail/GmailTransport.php b/app/Helpers/Mail/GmailTransport.php index e8641e8b8fd1..7d12362367bb 100644 --- a/app/Helpers/Mail/GmailTransport.php +++ b/app/Helpers/Mail/GmailTransport.php @@ -12,6 +12,7 @@ namespace App\Helpers\Mail; use App\Utils\TempFile; +use Dacastro4\LaravelGmail\Facade\LaravelGmail; use Dacastro4\LaravelGmail\Services\Message\Mail; use Illuminate\Mail\Transport\Transport; use Swift_Mime_SimpleMessage; @@ -43,17 +44,21 @@ class GmailTransport extends Transport { /*We should nest the token in the message and then discard it as needed*/ - $token = $message->getHeaders()->get('GmailToken'); + $token = $message->getHeaders()->get('GmailToken')->getValue(); + $user_id = $message->getHeaders()->get('UserId')->getValue(); + + LaravelGmail::setUserId($user_id); nlog("gmail transporter token = {$token}"); $message->getHeaders()->remove('GmailToken'); + $message->getHeaders()->remove('UserId'); nlog("inside gmail sender with token {$token}"); $this->beforeSendPerformed($message); - $this->gmail->using($this->token); + $this->gmail->using($token); $this->gmail->to($message->getTo()); $this->gmail->from($message->getFrom()); $this->gmail->subject($message->getSubject()); @@ -82,6 +87,7 @@ class GmailTransport extends Transport $this->sendPerformed($message); + return $this->numberOfRecipients($message); } } diff --git a/app/Jobs/Mail/BaseMailerJob.php b/app/Jobs/Mail/BaseMailerJob.php index fd34481b3582..c11abe32ccc2 100644 --- a/app/Jobs/Mail/BaseMailerJob.php +++ b/app/Jobs/Mail/BaseMailerJob.php @@ -83,10 +83,10 @@ class BaseMailerJob implements ShouldQueue * just for this request. */ - config(['mail.driver' => 'gmail']); - config(['services.gmail.token' => $user->oauth_user_token->access_token]); - config(['mail.from.address' => $user->email]); - config(['mail.from.name' => $user->present()->name()]); + // config(['mail.driver' => 'gmail']); + // config(['services.gmail.token' => $user->oauth_user_token->access_token]); + // config(['mail.from.address' => $user->email]); + // config(['mail.from.name' => $user->present()->name()]); //(new MailServiceProvider(app()))->register(); diff --git a/app/Jobs/Mail/NinjaMailerJob.php b/app/Jobs/Mail/NinjaMailerJob.php index 9a8733dec735..048c87787108 100644 --- a/app/Jobs/Mail/NinjaMailerJob.php +++ b/app/Jobs/Mail/NinjaMailerJob.php @@ -34,6 +34,7 @@ use Illuminate\Support\Facades\Config; use Illuminate\Support\Facades\Lang; use Illuminate\Support\Facades\Mail; use Turbo124\Beacon\Facades\LightLogs; +use Dacastro4\LaravelGmail\Facade\LaravelGmail; /*Multi Mailer implemented*/ @@ -105,12 +106,14 @@ class NinjaMailerJob implements ShouldQueue App::forgetInstance('translator'); App::forgetInstance('mail.manager'); //singletons must be destroyed! App::forgetInstance('mailer'); + App::forgetInstance('laravelgmail'); /* Inject custom translations if any exist */ Lang::replace(Ninja::transformTranslations($this->nmo->settings)); switch ($this->nmo->settings->email_sending_method) { case 'default': + config(['mail.driver' => config('mail.default')]); break; case 'gmail': $this->setGmailMailer(); @@ -118,10 +121,15 @@ class NinjaMailerJob implements ShouldQueue default: break; } + + (new MailServiceProvider(app()))->register(); } private function setGmailMailer() { + if(LaravelGmail::check()) + LaravelGmail::logout(); + $sending_user = $this->nmo->settings->gmail_sending_user_id; $user = User::find($this->decodePrimaryKey($sending_user)); @@ -141,22 +149,18 @@ class NinjaMailerJob implements ShouldQueue * just for this request. */ - // config(['mail.driver' => 'gmail']); - // config(['services.gmail.token' => $user->oauth_user_token->access_token]); - // config(['mail.from.address' => $user->email]); - // config(['mail.from.name' => $user->present()->name()]); - - // (new MailServiceProvider(app()))->register(); - - // nlog("after registering mail service provider"); - // nlog(config('services.gmail.token')); + config(['mail.driver' => 'gmail']); + (new MailServiceProvider(app()))->register(); $token = $user->oauth_user_token->access_token; + $user_id = $user->oauth_user_id; + $this->nmo ->mailable ->from($user->email, $user->present()->name()) - ->withSwiftMessage(function ($message) use($token) { - $message->getHeaders()->addTextHeader('GmailToken', $token); + ->withSwiftMessage(function ($message) use($token, $user_id) { + $message->getHeaders()->addTextHeader('GmailToken', $token); + $message->getHeaders()->addTextHeader('UserId', $user_id); }); } diff --git a/app/Listeners/SendVerificationNotification.php b/app/Listeners/SendVerificationNotification.php index 058130a5947b..1c396454dd1c 100644 --- a/app/Listeners/SendVerificationNotification.php +++ b/app/Listeners/SendVerificationNotification.php @@ -61,6 +61,7 @@ class SendVerificationNotification implements ShouldQueue // $event->user->notify(new VerifyUser($event->user, $event->company)); Ninja::registerNinjaUser($event->user); + } catch (Exception $e) { nlog("I couldn't send the email " . $e->getMessage()); } diff --git a/app/Mail/TemplateEmail.php b/app/Mail/TemplateEmail.php index 00d2a08f81c7..76c5b33732fb 100644 --- a/app/Mail/TemplateEmail.php +++ b/app/Mail/TemplateEmail.php @@ -75,7 +75,7 @@ class TemplateEmail extends Mailable ]) ->withSwiftMessage(function ($message) use($company){ $message->getHeaders()->addTextHeader('Tag', $company->company_key); - });; + }); //conditionally attach files if ($settings->pdf_email_attachment !== false && ! empty($this->build_email->getAttachments())) { diff --git a/config/gmail.php b/config/gmail.php new file mode 100644 index 000000000000..53f42725d8ee --- /dev/null +++ b/config/gmail.php @@ -0,0 +1,99 @@ + env('GOOGLE_PROJECT_ID'), + 'client_id' => env('GOOGLE_CLIENT_ID'), + 'client_secret' => env('GOOGLE_CLIENT_SECRET'), + 'redirect_url' => env('GOOGLE_REDIRECT_URI', '/'), + + 'state' => null, + + 'scopes' => [ + 'readonly', + 'modify', + ], + + /* + |-------------------------------------------------------------------------- + | Additional Scopes [URL Style] + |-------------------------------------------------------------------------- + | + | 'additional_scopes' => [ + | 'https://www.googleapis.com/auth/drive', + | 'https://www.googleapis.com/auth/documents' + | ], + | + | + */ + + 'additional_scopes' => [ + + ], + + 'access_type' => 'offline', + + 'approval_prompt' => 'force', + + /* + |-------------------------------------------------------------------------- + | Credentials File Name + |-------------------------------------------------------------------------- + | + | :email to use, clients email on the file + | + | + */ + + 'credentials_file_name' => env('GOOGLE_CREDENTIALS_NAME', 'gmail-json'), + + /* + |-------------------------------------------------------------------------- + | Allow Multiple Credentials + |-------------------------------------------------------------------------- + | + | Allow the application to store multiple credential json files. + | + | + */ + + 'allow_multiple_credentials' => env('GOOGLE_ALLOW_MULTIPLE_CREDENTIALS', false), + + /* + |-------------------------------------------------------------------------- + | Allow Encryption for json Files + |-------------------------------------------------------------------------- + | + | Use Laravel Encrypt in json Files + | + | + */ + + 'allow_json_encrypt' => env('GOOGLE_ALLOW_JSON_ENCRYPT', false), +];