company = $company; $this->invitation = $invitation; $this->settings = $invitation->contact->client->getMergedSettings(); $this->entity_string = $this->resolveEntityString(); $this->entity = $invitation->{$this->entity_string}; $this->reminder_template = $reminder_template ?: $this->entity->calculateTemplate($this->entity_string); $this->html_engine = new HtmlEngine($invitation); $this->template_data = $template_data; $this->email_entity_builder = $this->resolveEmailBuilder(); } /** * Execute the job. * * * @return void */ public function handle() { /* Don't fire emails if the company is disabled */ if ($this->company->is_disabled) return true; /* Set DB */ MultiDB::setDB($this->company->db); /* Set the correct mail driver */ $this->setMailDriver(); try { Mail::to($this->invitation->contact->email, $this->invitation->contact->present()->name()) ->send( new TemplateEmail( $this->email_entity_builder, $this->invitation->contact ) ); } catch (\Exception $e) { $this->entityEmailFailed($e->getMessage()); $this->logMailError($e->getMessage(), $this->entity->client); } /* Mark entity sent */ $this->entity->service()->markSent()->save(); } private function resolveEntityString() :string { if ($this->invitation instanceof InvoiceInvitation) { return 'invoice'; } elseif ($this->invitation instanceof QuoteInvitation) { return 'quote'; } elseif ($this->invitation instanceof CreditInvitation) { return 'credit'; } elseif ($this->invitation instanceof RecurringInvoiceInvitation) { return 'recurring_invoice'; } } /* Switch statement to handling failure notifications */ private function entityEmailFailed($message) { switch ($this->entity_string) { case 'invoice': event(new InvoiceWasEmailedAndFailed($this->invitation, $this->company, $message, $this->reminder_template, Ninja::eventVars())); break; default: # code... break; } } /* Builds the email builder object */ private function resolveEmailBuilder() { $class = 'App\Mail\Engine\\' . ucfirst(Str::camel($this->entity_string)) . "EmailEngine"; return (new $class($this->invitation, $this->reminder_template, $this->template_data))->build(); } }