1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-11-06 03:02:34 +01:00
invoiceninja/app/Jobs/Mail/MailRouter.php

84 lines
2.1 KiB
PHP
Raw Normal View History

<?php
/**
* Invoice Ninja (https://invoiceninja.com).
*
* @link https://github.com/invoiceninja/invoiceninja source repository
*
* @copyright Copyright (c) 2021. Invoice Ninja LLC (https://invoiceninja.com)
*
* @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;
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-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)
{
$this->mailable = $mailable;
$this->company = $company;
$this->to_user = $to_user;
$this->sending_method = $sending_method;
2020-08-10 06:55:44 +02:00
if ($to_user instanceof ClientContact) {
2020-08-10 06:55:44 +02:00
$this->settings = $to_user->client->getMergedSettings();
} else {
2020-08-10 06:55:44 +02:00
$this->settings = $this->company->settings;
}
}
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
MultiDB::setDb($this->company->db);
//if we need to set an email driver do it now
2020-08-10 06:55:44 +02:00
$this->setMailDriver();
//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) {
2020-11-12 10:41:19 +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
}
}
}
}