1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-09-21 08:51:34 +02:00
invoiceninja/app/Jobs/Mail/MailRouter.php
2020-11-01 14:19:03 +11:00

89 lines
2.3 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\Mail;
use App\Jobs\Mail\BaseMailerJob;
use App\Jobs\Util\SystemLogger;
use App\Libraries\Google\Google;
use App\Libraries\MultiDB;
use App\Mail\Admin\EntityNotificationMailer;
use App\Mail\Admin\EntitySentObject;
use App\Models\ClientContact;
use App\Models\Company;
use App\Models\SystemLog;
use App\Models\User;
use App\Providers\MailServiceProvider;
use Dacastro4\LaravelGmail\Services\Message\Mail;
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\Config;
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
public $sending_method; //not sure if we even need this
public $settings;
public function __construct(Mailable $mailable, Company $company, $to_user, $sending_method = null)
{
$this->mailable = $mailable;
$this->company = $company;
$this->to_user = $to_user;
$this->sending_method = $sending_method;
if ($to_user instanceof ClientContact) {
$this->settings = $to_user->client->getMergedSettings();
} else {
$this->settings = $this->company->settings;
}
}
public function handle()
{
/*If we are migrating data we don't want to fire these notification*/
if ($this->company->is_disabled)
return true;
MultiDB::setDb($this->company->db);
//if we need to set an email driver do it now
$this->setMailDriver();
//send email
Mail::to($this->to_user->email)
->send($this->mailable);
//catch errors
if (count(Mail::failures()) > 0) {
$this->logMailError(Mail::failures(), $this->to_user);
}
}
}