From 6fd5d436c7c8794455fbf6f5518164c6217ce781 Mon Sep 17 00:00:00 2001 From: David Bomba Date: Sun, 14 Feb 2021 21:43:44 +1100 Subject: [PATCH] Refactoring mailers --- app/Jobs/Mail/NinjaMailer.php | 46 ++++++++ app/Jobs/Mail/NinjaMailerJob.php | 154 +++++++++++++++++++++++++ app/Jobs/Mail/NinjaMailerObject.php | 32 +++++ app/Jobs/Mail/PaymentFailureMailer.php | 28 ++--- 4 files changed, 243 insertions(+), 17 deletions(-) create mode 100644 app/Jobs/Mail/NinjaMailer.php create mode 100644 app/Jobs/Mail/NinjaMailerJob.php create mode 100644 app/Jobs/Mail/NinjaMailerObject.php diff --git a/app/Jobs/Mail/NinjaMailer.php b/app/Jobs/Mail/NinjaMailer.php new file mode 100644 index 000000000000..a5e68c4e480e --- /dev/null +++ b/app/Jobs/Mail/NinjaMailer.php @@ -0,0 +1,46 @@ +mail_obj = $mail_obj; + } + + /** + * Build the message. + * + * @return $this + */ + public function build() + { + + return $this->from(config('mail.from.address'), config('mail.from.name')) + ->subject($this->mail_obj->subject) + ->markdown($this->mail_obj->markdown, $this->mail_obj->data) + ->withSwiftMessage(function ($message) { + $message->getHeaders()->addTextHeader('Tag', $this->mail_obj->tag); + }); + + } +} diff --git a/app/Jobs/Mail/NinjaMailerJob.php b/app/Jobs/Mail/NinjaMailerJob.php new file mode 100644 index 000000000000..cbce762bd73c --- /dev/null +++ b/app/Jobs/Mail/NinjaMailerJob.php @@ -0,0 +1,154 @@ +nmo = $nmo; + + } + + public function handle() + { + /*If we are migrating data we don't want to fire any emails*/ + if ($this->nmo->company->is_disabled) + return true; + + MultiDB::setDb($this->nmo->company->db); + + //if we need to set an email driver do it now + $this->setMailDriver(); + + //send email + try { + Mail::to($this->nmo->to_user->email) + ->send($this->nmo->mailable); + } catch (\Exception $e) { + //$this->failed($e); + + if ($this->nmo->to_user instanceof ClientContact) { + $this->logMailError($e->getMessage(), $this->nmo->to_user->client); + } + } + } + + private function setMailDriver() + { + /* Singletons need to be rebooted each time just in case our Locale is changing*/ + App::forgetInstance('translator'); + App::forgetInstance('mail.manager'); //singletons must be destroyed! + + /* Inject custom translations if any exist */ + Lang::replace(Ninja::transformTranslations($this->nmo->settings)); + + switch ($this->nmo->settings->email_sending_method) { + case 'default': + break; + case 'gmail': + $this->setGmailMailer(); + break; + default: + break; + } + } + + private function setGmailMailer() + { + $sending_user = $this->settings->gmail_sending_user_id; + + $user = User::find($this->decodePrimaryKey($sending_user)); + + $google = (new Google())->init(); + $google->getClient()->setAccessToken(json_encode($user->oauth_user_token)); + + if ($google->getClient()->isAccessTokenExpired()) { + $google->refreshToken($user); + } + + /* + * Now that our token is refreshed and valid we can boot the + * mail driver at runtime and also set the token which will persist + * 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')); + } + + private function logMailError($errors, $recipient_object) + { + SystemLogger::dispatch( + $errors, + SystemLog::CATEGORY_MAIL, + SystemLog::EVENT_MAIL_SEND, + SystemLog::TYPE_FAILURE, + $recipient_object + ); + } + + private function failed($exception = null) + { + nlog('mailer job failed'); + nlog($exception->getMessage()); + + $job_failure = new EmailFailure(); + $job_failure->string_metric5 = get_parent_class($this); + $job_failure->string_metric6 = $exception->getMessage(); + + LightLogs::create($job_failure) + ->batch(); + } +} diff --git a/app/Jobs/Mail/NinjaMailerObject.php b/app/Jobs/Mail/NinjaMailerObject.php new file mode 100644 index 000000000000..55756083d190 --- /dev/null +++ b/app/Jobs/Mail/NinjaMailerObject.php @@ -0,0 +1,32 @@ +company->is_disabled) { - return true; - } - //Set DB MultiDB::setDb($this->company->db); - //if we need to set an email driver do it now - $this->setMailDriver(); - //iterate through company_users $this->company->company_users->each(function ($company_user) { @@ -93,16 +88,15 @@ class PaymentFailureMailer extends BaseMailerJob implements ShouldQueue unset($methods[$key]); $mail_obj = (new PaymentFailureObject($this->client, $this->error, $this->company, $this->payment_hash))->build(); - $mail_obj->from = [config('mail.from.address'), config('mail.from.name')]; - //send email - try { - Mail::to($company_user->user->email) - ->send(new EntityNotificationMailer($mail_obj)); - } catch (\Exception $e) { - //$this->failed($e); - $this->logMailError($e->getMessage(), $this->client); - } + $nmo = new NinjaMailerObject; + $nmo->mailable = new NinjaMailer($mail_obj); + $nmo->company = $this->companyl; + $nmo->to_user = $company_user->user; + $nmo->settings = $this->settings; + + NinjaMailerJob::dispatch($nmo); + } }); }