From 0b8bf4fbf1e4138b6d7090765732f8c080b5322f Mon Sep 17 00:00:00 2001 From: David Bomba Date: Wed, 17 Feb 2021 00:31:00 +1100 Subject: [PATCH 1/2] Remove invoice deletion observer --- app/Jobs/Entity/EmailEntity.php | 5 +---- app/Observers/InvoiceObserver.php | 2 +- app/Services/Credit/ApplyPayment.php | 2 +- app/Services/Invoice/InvoiceService.php | 2 +- 4 files changed, 4 insertions(+), 7 deletions(-) diff --git a/app/Jobs/Entity/EmailEntity.php b/app/Jobs/Entity/EmailEntity.php index 5711d5dccdef..3c35ba56d5aa 100644 --- a/app/Jobs/Entity/EmailEntity.php +++ b/app/Jobs/Entity/EmailEntity.php @@ -38,7 +38,7 @@ use Illuminate\Support\Str; /*Multi Mailer implemented*/ -class EmailEntity extends BaseMailerJob implements ShouldQueue +class EmailEntity implements ShouldQueue { use Dispatchable, InteractsWithQueue, Queueable, SerializesModels; @@ -105,9 +105,6 @@ class EmailEntity extends BaseMailerJob implements ShouldQueue /* Set DB */ MultiDB::setDB($this->company->db); - /* Set the correct mail driver */ - $this->setMailDriver(); - $nmo = new NinjaMailerObject; $nmo->mailable = new TemplateEmail($this->email_entity_builder,$this->invitation->contact); $nmo->company = $this->company; diff --git a/app/Observers/InvoiceObserver.php b/app/Observers/InvoiceObserver.php index 14f5aa9c68de..260629be76d5 100644 --- a/app/Observers/InvoiceObserver.php +++ b/app/Observers/InvoiceObserver.php @@ -52,7 +52,7 @@ class InvoiceObserver WebhookHandler::dispatch(Webhook::EVENT_UPDATE_INVOICE, $invoice, $invoice->company); } - UnlinkFile::dispatchNow(config('filesystems.default'), $invoice->client->invoice_filepath() . $invoice->number.'.pdf'); + // UnlinkFile::dispatchNow(config('filesystems.default'), $invoice->client->invoice_filepath() . $invoice->number.'.pdf'); } diff --git a/app/Services/Credit/ApplyPayment.php b/app/Services/Credit/ApplyPayment.php index 40a6652e1e71..e97e38c3854b 100644 --- a/app/Services/Credit/ApplyPayment.php +++ b/app/Services/Credit/ApplyPayment.php @@ -147,7 +147,7 @@ class ApplyPayment event(new InvoiceWasUpdated($this->invoice, $this->invoice->company, Ninja::eventVars())); if ((int)$this->invoice->balance == 0) { - // $this->invoice->service()->deletePdf(); + $this->invoice->service()->deletePdf(); event(new InvoiceWasPaid($this->invoice, $payment, $this->payment->company, Ninja::eventVars())); } } diff --git a/app/Services/Invoice/InvoiceService.php b/app/Services/Invoice/InvoiceService.php index ccaa2e929954..b45e5eb19b6e 100644 --- a/app/Services/Invoice/InvoiceService.php +++ b/app/Services/Invoice/InvoiceService.php @@ -266,7 +266,7 @@ class InvoiceService //$this->invoice = $this->invoice->calc()->getInvoice(); - // $this->deletePdf(); + $this->deletePdf(); return $this; } From aa9970326cf0e83c9e785608c01d52f2bbf205d1 Mon Sep 17 00:00:00 2001 From: David Bomba Date: Wed, 17 Feb 2021 11:25:30 +1100 Subject: [PATCH 2/2] 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), +];