diff --git a/app/Helpers/Email/EmailBuilder.php b/app/Helpers/Email/EmailBuilder.php index aa7d19bcaa10..1be706e54b02 100644 --- a/app/Helpers/Email/EmailBuilder.php +++ b/app/Helpers/Email/EmailBuilder.php @@ -92,8 +92,7 @@ class EmailBuilder */ public function setBody($body) { - //$this->body = $this->parseTemplate($body, true); - + //todo move this to use HTMLEngine if (! empty($this->variables)) { $body = str_replace(array_keys($this->variables), array_values($this->variables), $body); } diff --git a/app/Helpers/Email/EntityEmailInterface.php b/app/Helpers/Email/EntityEmailInterface.php new file mode 100644 index 000000000000..bba93573eb20 --- /dev/null +++ b/app/Helpers/Email/EntityEmailInterface.php @@ -0,0 +1,17 @@ +company = $company; + + $this->invitation = $invitation; + + $this->email_builder = $email_builder; + + $this->settings = $invitation->contact->client->getMergedSettings(); + + $this->entity_string = $this->resolveEntityString(); + } + + /** + * Execute the job. + * + * + * @return void + */ + public function handle() + { + + MultiDB::setDB($this->company->db); + + $this->setMailDriver(); + + try { + Mail::to($this->invitation->contact->email, $this->invitation->contact->present()->name()) + ->send( + new TemplateEmail( + $this->email_builder, + $this->invitation->contact->user, + $this->invitation->contact->client + ) + ); + } catch (\Swift_TransportException $e) { + $this->entityEmailFailed($e->getMessage()); + } + + if (count(Mail::failures()) > 0) { + $this->logMailError(Mail::failures(), $this->invoice->client); + } else { + $this->entityEmailSucceeded(); + } + + /* Mark invoice sent */ + $this->invitation->invoice->service()->markSent()->save(); + } + + public function failed($exception = null) + { + info('the job failed'); + + $job_failure = new EmailInvoiceFailure(); + $job_failure->string_metric5 = get_class($this); + $job_failure->string_metric6 = $exception->getMessage(); + + LightLogs::create($job_failure) + ->batch(); + + } + + 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'; + } + + private function entityEmailFailed($message) + { + switch ($this->entity_string) { + case 'invoice': + event(new InvoiceWasEmailedAndFailed($this->invitation->invoice, $this->company, $message, Ninja::eventVars())); + break; + + default: + # code... + break; + } + + } + + private function entityEmailSucceeded() + { + switch ($this->entity_string) { + case 'invoice': + event(new InvoiceWasEmailed($this->invitation, $this->company, Ninja::eventVars())); + break; + + default: + # code... + break; + } + } +} diff --git a/app/Jobs/Invoice/EmailInvoice.php b/app/Jobs/Invoice/EmailInvoice.php index 4f84dd975a27..99d3979bc8a2 100644 --- a/app/Jobs/Invoice/EmailInvoice.php +++ b/app/Jobs/Invoice/EmailInvoice.php @@ -70,7 +70,7 @@ class EmailInvoice extends BaseMailerJob implements ShouldQueue * @return void */ public function handle() - { + {info("invoice email"); MultiDB::setDB($this->company->db); $this->setMailDriver();