1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-09-21 17:01:33 +02:00
invoiceninja/app/Jobs/Invoice/EmailInvoice.php
David Bomba 4a3d37a42b
Flutter Client ! (#3325)
* Working on emailing invoices

* Working on emailing and displaying email

* Working on emailing and displaying email

* Email invoices

* Fixes for html emails

* Restart queue after self-update

* Email Invoices

* Push Flutter Web Clientgit statusgit status!
2020-02-13 22:27:42 +11:00

109 lines
3.2 KiB
PHP

<?php
/**
* Invoice Ninja (https://invoiceninja.com)
*
* @link https://github.com/invoiceninja/invoiceninja source repository
*
* @copyright Copyright (c) 2020. Invoice Ninja LLC (https://invoiceninja.com)
*
* @license https://opensource.org/licenses/AAL
*/
namespace App\Jobs\Invoice;
use App\Events\Invoice\InvoiceWasEmailed;
use App\Events\Invoice\InvoiceWasEmailedAndFailed;
use App\Libraries\MultiDB;
use App\Mail\TemplateEmail;
use App\Models\Company;
use App\Models\Invoice;
use App\Models\SystemLog;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
use Illuminate\Support\Facades\Mail;
class EmailInvoice implements ShouldQueue
{
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
public $invoice;
public $message_array = [];
private $company;
/**
* Create a new job instance.
*
* @return void
*/
public function __construct(Invoice $invoice, Company $company)
{
$this->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();
$message_array['footer'] = "<a href='{$invitation->getLink()}'>Invoice Link</a>";
//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
);
}
}