2020-01-30 02:27:22 +01:00
|
|
|
<?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
|
|
|
|
*/
|
|
|
|
|
2020-04-06 14:32:27 +02:00
|
|
|
namespace App\Jobs\Credit;
|
2020-01-30 02:27:22 +01:00
|
|
|
|
|
|
|
use App\Events\Credit\CreditWasEmailed;
|
|
|
|
use App\Events\Credit\CreditWasEmailedAndFailed;
|
2020-08-06 05:04:09 +02:00
|
|
|
use App\Jobs\Mail\BaseMailerJob;
|
2020-01-30 02:27:22 +01:00
|
|
|
use App\Jobs\Util\SystemLogger;
|
|
|
|
use App\Libraries\MultiDB;
|
|
|
|
use App\Mail\TemplateEmail;
|
|
|
|
use App\Models\Credit;
|
|
|
|
use App\Models\SystemLog;
|
2020-07-08 14:02:16 +02:00
|
|
|
use App\Utils\Ninja;
|
2020-01-30 02:27:22 +01:00
|
|
|
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;
|
|
|
|
|
2020-08-06 05:04:09 +02:00
|
|
|
/*Multi Mailer implemented*/
|
|
|
|
class EmailCredit extends BaseMailerJob implements ShouldQueue
|
2020-01-30 02:27:22 +01:00
|
|
|
{
|
|
|
|
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
|
|
|
|
|
|
|
|
public $credit;
|
|
|
|
|
|
|
|
public $message_array = [];
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Create a new job instance.
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
2020-02-24 11:15:30 +01:00
|
|
|
public function __construct(Credit $credit)
|
2020-01-30 02:27:22 +01:00
|
|
|
{
|
|
|
|
$this->credit = $credit;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Execute the job.
|
|
|
|
*
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function handle()
|
|
|
|
{
|
|
|
|
//todo - change runtime config of mail driver if necessary
|
|
|
|
|
|
|
|
$template_style = $this->credit->client->getSetting('email_style');
|
|
|
|
|
2020-08-06 05:04:09 +02:00
|
|
|
$this->setMailDriver($this->credit->client->getSetting('email_sending_method'));
|
|
|
|
|
2020-01-30 02:27:22 +01:00
|
|
|
$this->credit->invitations->each(function ($invitation) use ($template_style) {
|
2020-08-06 05:04:09 +02:00
|
|
|
|
2020-04-30 13:45:47 +02:00
|
|
|
if ($invitation->contact->send_email && $invitation->contact->email) {
|
2020-08-06 05:04:09 +02:00
|
|
|
|
2020-01-30 02:27:22 +01:00
|
|
|
$message_array = $this->credit->getEmailData('', $invitation->contact);
|
|
|
|
$message_array['title'] = &$message_array['subject'];
|
|
|
|
$message_array['footer'] = "Sent to ".$invitation->contact->present()->name();
|
2020-08-06 05:04:09 +02:00
|
|
|
|
2020-01-30 02:27:22 +01:00
|
|
|
//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) {
|
2020-07-08 14:02:16 +02:00
|
|
|
event(new CreditWasEmailedAndFailed($this->credit, $this->credit->company, Mail::failures(), Ninja::eventVars()));
|
2020-01-30 02:27:22 +01:00
|
|
|
|
|
|
|
return $this->logMailError($errors);
|
|
|
|
}
|
|
|
|
|
|
|
|
//fire any events
|
2020-07-08 14:02:16 +02:00
|
|
|
event(new CreditWasEmailed($this->credit, $this->company, Ninja::eventVars()));
|
2020-01-30 02:27:22 +01:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
private function logMailError($errors)
|
|
|
|
{
|
|
|
|
SystemLogger::dispatch(
|
|
|
|
$errors,
|
|
|
|
SystemLog::CATEGORY_MAIL,
|
|
|
|
SystemLog::EVENT_MAIL_SEND,
|
|
|
|
SystemLog::TYPE_FAILURE,
|
|
|
|
$this->credit->client
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|