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; /*Set the correct database*/ MultiDB::setDb($this->nmo->company->db); /* Set the email driver */ $this->setMailDriver(); if (strlen($this->nmo->settings->reply_to_email) > 1) { $reply_to_name = strlen($this->nmo->settings->reply_to_name) > 1 ? $this->nmo->settings->reply_to_name : $this->nmo->company->present()->name(); $this->nmo->mailable->replyTo($this->nmo->settings->reply_to_email, $reply_to_name); } if (strlen($this->nmo->settings->bcc_email) > 1) $this->nmo->mailable->bcc($this->nmo->settings->bcc_email, $this->nmo->settings->bcc_email); //send email try { nlog("trying to send"); Mail::to($this->nmo->to_user->email) ->send($this->nmo->mailable); } catch (\Exception $e) { nlog("error failed with {$e->getMessage()}"); // nlog($e); if($this->nmo->entity) $this->entityEmailFailed($e->getMessage()); } } /* Switch statement to handle failure notifications */ private function entityEmailFailed($message) { $class = get_class($this->nmo->entity); switch ($class) { case Invoice::class: event(new InvoiceWasEmailedAndFailed($this->nmo->invitation, $this->nmo->company, $message, $this->nmo->reminder_template, Ninja::eventVars(auth()->user() ? auth()->user()->id : null))); break; case Payment::class: event(new PaymentWasEmailedAndFailed($this->nmo->entity, $this->nmo->company, $message, Ninja::eventVars(auth()->user ? auth()->user()->id : null))); break; default: # code... break; } if ($this->nmo->to_user instanceof ClientContact) $this->logMailError($message, $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! 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(); break; 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)); nlog("Sending via {$user->name()}"); $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']); (new MailServiceProvider(app()))->register(); $token = $user->oauth_user_token->access_token; $this->nmo ->mailable ->from($user->email, $user->name()) ->withSwiftMessage(function ($message) use($token) { $message->getHeaders()->addTextHeader('GmailToken', $token); }); } private function logMailError($errors, $recipient_object) { SystemLogger::dispatch( $errors, SystemLog::CATEGORY_MAIL, SystemLog::EVENT_MAIL_SEND, SystemLog::TYPE_FAILURE, $recipient_object ); } public 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(); } }