invoice = $invoice; $this->company = $company; } /** * Execute the job. * * * @return void */ public function handle() { /*Jobs are not multi-db aware, need to set! */ MultiDB::setDB($this->company->db); //todo - change runtime config of mail driver if necessary $template_style = $this->invoice->client->getSetting('email_style'); $this->invoice->invitations->each(function ($invitation) use ($template_style) { if ($invitation->contact->send_invoice && $invitation->contact->email) { $message_array = $this->invoice->getEmailData('', $invitation->contact); $message_array['title'] = &$message_array['subject']; $message_array['footer'] = "Sent to ".$invitation->contact->present()->name(); //change the runtime config of the mail provider here: //send message Mail::to($invitation->contact->email, $invitation->contact->present()->name()) ->send(new TemplateEmail($message_array, $template_style, $invitation->contact->user, $invitation->contact->client)); if (count(Mail::failures()) > 0) { event(new InvoiceWasEmailedAndFailed($this->invoice, Mail::failures())); return $this->logMailError($errors); } //fire any events event(new InvoiceWasEmailed($this->invoice)); //sleep(5); } }); } private function logMailError($errors) { SystemLogger::dispatch( $errors, SystemLog::CATEGORY_MAIL, SystemLog::EVENT_MAIL_SEND, SystemLog::TYPE_FAILURE, $this->invoice->client ); } }