2020-08-08 01:28:36 +02:00
|
|
|
<?php
|
|
|
|
/**
|
2020-09-06 11:38:10 +02:00
|
|
|
* Invoice Ninja (https://invoiceninja.com).
|
2020-08-08 01:28:36 +02:00
|
|
|
*
|
|
|
|
* @link https://github.com/invoiceninja/invoiceninja source repository
|
|
|
|
*
|
2021-01-03 22:54:54 +01:00
|
|
|
* @copyright Copyright (c) 2021. Invoice Ninja LLC (https://invoiceninja.com)
|
2020-08-08 01:28:36 +02:00
|
|
|
*
|
|
|
|
* @license https://opensource.org/licenses/AAL
|
|
|
|
*/
|
|
|
|
|
|
|
|
namespace App\Jobs\Mail;
|
|
|
|
|
|
|
|
use App\Libraries\MultiDB;
|
2020-08-10 06:55:44 +02:00
|
|
|
use App\Models\ClientContact;
|
2020-08-08 01:28:36 +02:00
|
|
|
use App\Models\Company;
|
|
|
|
use App\Models\User;
|
|
|
|
use Illuminate\Bus\Queueable;
|
|
|
|
use Illuminate\Contracts\Queue\ShouldQueue;
|
|
|
|
use Illuminate\Foundation\Bus\Dispatchable;
|
|
|
|
use Illuminate\Mail\Mailable;
|
|
|
|
use Illuminate\Queue\InteractsWithQueue;
|
|
|
|
use Illuminate\Queue\SerializesModels;
|
|
|
|
use Illuminate\Support\Facades\Mail;
|
|
|
|
|
|
|
|
/*Multi Mailer Router implemented*/
|
|
|
|
|
|
|
|
class MailRouter extends BaseMailerJob implements ShouldQueue
|
|
|
|
{
|
|
|
|
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
|
|
|
|
|
|
|
|
public $mailable;
|
|
|
|
|
|
|
|
public $company;
|
|
|
|
|
|
|
|
public $to_user; //User or ClientContact
|
|
|
|
|
2020-09-14 07:14:37 +02:00
|
|
|
public $sending_method; //not sure if we even need this
|
2020-08-08 01:28:36 +02:00
|
|
|
|
2020-08-10 06:55:44 +02:00
|
|
|
public $settings;
|
|
|
|
|
2020-09-14 07:14:37 +02:00
|
|
|
public function __construct(Mailable $mailable, Company $company, $to_user, $sending_method = null)
|
2020-08-08 01:28:36 +02:00
|
|
|
{
|
|
|
|
$this->mailable = $mailable;
|
|
|
|
|
|
|
|
$this->company = $company;
|
|
|
|
|
|
|
|
$this->to_user = $to_user;
|
|
|
|
|
|
|
|
$this->sending_method = $sending_method;
|
2020-08-10 06:55:44 +02:00
|
|
|
|
2020-09-06 11:38:10 +02:00
|
|
|
if ($to_user instanceof ClientContact) {
|
2020-08-10 06:55:44 +02:00
|
|
|
$this->settings = $to_user->client->getMergedSettings();
|
2020-09-06 11:38:10 +02:00
|
|
|
} else {
|
2020-08-10 06:55:44 +02:00
|
|
|
$this->settings = $this->company->settings;
|
2020-09-06 11:38:10 +02:00
|
|
|
}
|
2020-08-08 01:28:36 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public function handle()
|
|
|
|
{
|
2020-11-01 04:19:03 +01:00
|
|
|
/*If we are migrating data we don't want to fire these notification*/
|
2020-11-25 15:19:52 +01:00
|
|
|
if ($this->company->is_disabled) {
|
2020-11-01 04:19:03 +01:00
|
|
|
return true;
|
2020-11-25 15:19:52 +01:00
|
|
|
}
|
2020-11-01 04:19:03 +01:00
|
|
|
|
2020-09-06 11:38:10 +02:00
|
|
|
MultiDB::setDb($this->company->db);
|
|
|
|
|
2020-08-08 01:28:36 +02:00
|
|
|
//if we need to set an email driver do it now
|
2020-08-10 06:55:44 +02:00
|
|
|
$this->setMailDriver();
|
2020-09-06 11:38:10 +02:00
|
|
|
|
2020-08-08 01:28:36 +02:00
|
|
|
//send email
|
2020-11-12 10:41:19 +01:00
|
|
|
try {
|
|
|
|
Mail::to($this->to_user->email)
|
|
|
|
->send($this->mailable);
|
2020-11-25 15:19:52 +01:00
|
|
|
} catch (\Exception $e) {
|
2021-01-21 00:02:39 +01:00
|
|
|
//$this->failed($e);
|
2020-12-21 02:16:26 +01:00
|
|
|
|
2020-12-23 11:41:57 +01:00
|
|
|
if ($this->to_user instanceof ClientContact) {
|
2020-12-21 02:16:26 +01:00
|
|
|
$this->logMailError($e->getMessage(), $this->to_user->client);
|
2020-12-23 11:41:57 +01:00
|
|
|
}
|
2020-08-08 01:28:36 +02:00
|
|
|
}
|
|
|
|
}
|
2020-09-06 11:38:10 +02:00
|
|
|
}
|