mirror of
https://github.com/invoiceninja/invoiceninja.git
synced 2025-07-09 03:14:30 -04:00
Refactoring mailers
This commit is contained in:
parent
862e86b8c9
commit
6fd5d436c7
46
app/Jobs/Mail/NinjaMailer.php
Normal file
46
app/Jobs/Mail/NinjaMailer.php
Normal file
@ -0,0 +1,46 @@
|
|||||||
|
<?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\Mail;
|
||||||
|
|
||||||
|
use Illuminate\Mail\Mailable;
|
||||||
|
|
||||||
|
class NinjaMailer extends Mailable
|
||||||
|
{
|
||||||
|
public $mail_obj;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Create a new message instance.
|
||||||
|
*
|
||||||
|
* @param $mail_obj
|
||||||
|
*/
|
||||||
|
public function __construct($mail_obj)
|
||||||
|
{
|
||||||
|
$this->mail_obj = $mail_obj;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Build the message.
|
||||||
|
*
|
||||||
|
* @return $this
|
||||||
|
*/
|
||||||
|
public function build()
|
||||||
|
{
|
||||||
|
|
||||||
|
return $this->from(config('mail.from.address'), config('mail.from.name'))
|
||||||
|
->subject($this->mail_obj->subject)
|
||||||
|
->markdown($this->mail_obj->markdown, $this->mail_obj->data)
|
||||||
|
->withSwiftMessage(function ($message) {
|
||||||
|
$message->getHeaders()->addTextHeader('Tag', $this->mail_obj->tag);
|
||||||
|
});
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
154
app/Jobs/Mail/NinjaMailerJob.php
Normal file
154
app/Jobs/Mail/NinjaMailerJob.php
Normal file
@ -0,0 +1,154 @@
|
|||||||
|
<?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\DataMapper\Analytics\EmailFailure;
|
||||||
|
use App\Jobs\Mail\NinjaMailerObject;
|
||||||
|
use App\Jobs\Util\SystemLogger;
|
||||||
|
use App\Libraries\Google\Google;
|
||||||
|
use App\Libraries\MultiDB;
|
||||||
|
use App\Models\ClientContact;
|
||||||
|
use App\Models\SystemLog;
|
||||||
|
use App\Models\User;
|
||||||
|
use App\Providers\MailServiceProvider;
|
||||||
|
use App\Utils\Ninja;
|
||||||
|
use App\Utils\Traits\MakesHash;
|
||||||
|
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\App;
|
||||||
|
use Illuminate\Support\Facades\Config;
|
||||||
|
use Illuminate\Support\Facades\Lang;
|
||||||
|
use Illuminate\Support\Facades\Mail;
|
||||||
|
use Turbo124\Beacon\Facades\LightLogs;
|
||||||
|
|
||||||
|
/*Multi Mailer implemented*/
|
||||||
|
|
||||||
|
class NinjaMailerJob implements ShouldQueue
|
||||||
|
{
|
||||||
|
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels, MakesHash;
|
||||||
|
|
||||||
|
public $tries = 5; //number of retries
|
||||||
|
|
||||||
|
public $backoff = 5; //seconds to wait until retry
|
||||||
|
|
||||||
|
public $deleteWhenMissingModels = true;
|
||||||
|
|
||||||
|
public $nmo;
|
||||||
|
|
||||||
|
public function __construct(NinjaMailerObject $nmo)
|
||||||
|
{
|
||||||
|
|
||||||
|
$this->nmo = $nmo;
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
public function handle()
|
||||||
|
{
|
||||||
|
/*If we are migrating data we don't want to fire any emails*/
|
||||||
|
if ($this->nmo->company->is_disabled)
|
||||||
|
return true;
|
||||||
|
|
||||||
|
MultiDB::setDb($this->nmo->company->db);
|
||||||
|
|
||||||
|
//if we need to set an email driver do it now
|
||||||
|
$this->setMailDriver();
|
||||||
|
|
||||||
|
//send email
|
||||||
|
try {
|
||||||
|
Mail::to($this->nmo->to_user->email)
|
||||||
|
->send($this->nmo->mailable);
|
||||||
|
} catch (\Exception $e) {
|
||||||
|
//$this->failed($e);
|
||||||
|
|
||||||
|
if ($this->nmo->to_user instanceof ClientContact) {
|
||||||
|
$this->logMailError($e->getMessage(), $this->nmo->to_user->client);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private function setMailDriver()
|
||||||
|
{
|
||||||
|
/* Singletons need to be rebooted each time just in case our Locale is changing*/
|
||||||
|
App::forgetInstance('translator');
|
||||||
|
App::forgetInstance('mail.manager'); //singletons must be destroyed!
|
||||||
|
|
||||||
|
/* Inject custom translations if any exist */
|
||||||
|
Lang::replace(Ninja::transformTranslations($this->nmo->settings));
|
||||||
|
|
||||||
|
switch ($this->nmo->settings->email_sending_method) {
|
||||||
|
case 'default':
|
||||||
|
break;
|
||||||
|
case 'gmail':
|
||||||
|
$this->setGmailMailer();
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private function setGmailMailer()
|
||||||
|
{
|
||||||
|
$sending_user = $this->settings->gmail_sending_user_id;
|
||||||
|
|
||||||
|
$user = User::find($this->decodePrimaryKey($sending_user));
|
||||||
|
|
||||||
|
$google = (new Google())->init();
|
||||||
|
$google->getClient()->setAccessToken(json_encode($user->oauth_user_token));
|
||||||
|
|
||||||
|
if ($google->getClient()->isAccessTokenExpired()) {
|
||||||
|
$google->refreshToken($user);
|
||||||
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Now that our token is refreshed and valid we can boot the
|
||||||
|
* mail driver at runtime and also set the token which will persist
|
||||||
|
* just for this request.
|
||||||
|
*/
|
||||||
|
|
||||||
|
config(['mail.driver' => 'gmail']);
|
||||||
|
config(['services.gmail.token' => $user->oauth_user_token->access_token]);
|
||||||
|
config(['mail.from.address' => $user->email]);
|
||||||
|
config(['mail.from.name' => $user->present()->name()]);
|
||||||
|
|
||||||
|
//(new MailServiceProvider(app()))->register();
|
||||||
|
|
||||||
|
nlog("after registering mail service provider");
|
||||||
|
nlog(config('services.gmail.token'));
|
||||||
|
}
|
||||||
|
|
||||||
|
private function logMailError($errors, $recipient_object)
|
||||||
|
{
|
||||||
|
SystemLogger::dispatch(
|
||||||
|
$errors,
|
||||||
|
SystemLog::CATEGORY_MAIL,
|
||||||
|
SystemLog::EVENT_MAIL_SEND,
|
||||||
|
SystemLog::TYPE_FAILURE,
|
||||||
|
$recipient_object
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
private function failed($exception = null)
|
||||||
|
{
|
||||||
|
nlog('mailer job failed');
|
||||||
|
nlog($exception->getMessage());
|
||||||
|
|
||||||
|
$job_failure = new EmailFailure();
|
||||||
|
$job_failure->string_metric5 = get_parent_class($this);
|
||||||
|
$job_failure->string_metric6 = $exception->getMessage();
|
||||||
|
|
||||||
|
LightLogs::create($job_failure)
|
||||||
|
->batch();
|
||||||
|
}
|
||||||
|
}
|
32
app/Jobs/Mail/NinjaMailerObject.php
Normal file
32
app/Jobs/Mail/NinjaMailerObject.php
Normal file
@ -0,0 +1,32 @@
|
|||||||
|
<?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;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* NinjaMailerObject.
|
||||||
|
*/
|
||||||
|
class NinjaMailerObject
|
||||||
|
{
|
||||||
|
|
||||||
|
public $mailable;
|
||||||
|
|
||||||
|
public $company;
|
||||||
|
|
||||||
|
public $from_user; //not yet used
|
||||||
|
|
||||||
|
public $to_user;
|
||||||
|
|
||||||
|
public $settings;
|
||||||
|
|
||||||
|
public $transport; //not yet used
|
||||||
|
|
||||||
|
}
|
@ -11,9 +11,12 @@
|
|||||||
|
|
||||||
namespace App\Jobs\Mail;
|
namespace App\Jobs\Mail;
|
||||||
|
|
||||||
|
use App\Jobs\Mail\NinjaMailerJob;
|
||||||
|
use App\Jobs\Mail\NinjaMailerObject;
|
||||||
use App\Libraries\MultiDB;
|
use App\Libraries\MultiDB;
|
||||||
use App\Mail\Admin\EntityNotificationMailer;
|
use App\Mail\Admin\EntityNotificationMailer;
|
||||||
use App\Mail\Admin\PaymentFailureObject;
|
use App\Mail\Admin\PaymentFailureObject;
|
||||||
|
use App\Mail\NinjaMailer;
|
||||||
use App\Models\User;
|
use App\Models\User;
|
||||||
use App\Utils\Traits\Notifications\UserNotifies;
|
use App\Utils\Traits\Notifications\UserNotifies;
|
||||||
use Illuminate\Bus\Queueable;
|
use Illuminate\Bus\Queueable;
|
||||||
@ -70,17 +73,9 @@ class PaymentFailureMailer extends BaseMailerJob implements ShouldQueue
|
|||||||
public function handle()
|
public function handle()
|
||||||
{
|
{
|
||||||
|
|
||||||
/*If we are migrating data we don't want to fire these notification*/
|
|
||||||
if ($this->company->is_disabled) {
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
//Set DB
|
//Set DB
|
||||||
MultiDB::setDb($this->company->db);
|
MultiDB::setDb($this->company->db);
|
||||||
|
|
||||||
//if we need to set an email driver do it now
|
|
||||||
$this->setMailDriver();
|
|
||||||
|
|
||||||
//iterate through company_users
|
//iterate through company_users
|
||||||
$this->company->company_users->each(function ($company_user) {
|
$this->company->company_users->each(function ($company_user) {
|
||||||
|
|
||||||
@ -93,16 +88,15 @@ class PaymentFailureMailer extends BaseMailerJob implements ShouldQueue
|
|||||||
unset($methods[$key]);
|
unset($methods[$key]);
|
||||||
|
|
||||||
$mail_obj = (new PaymentFailureObject($this->client, $this->error, $this->company, $this->payment_hash))->build();
|
$mail_obj = (new PaymentFailureObject($this->client, $this->error, $this->company, $this->payment_hash))->build();
|
||||||
$mail_obj->from = [config('mail.from.address'), config('mail.from.name')];
|
|
||||||
|
|
||||||
//send email
|
$nmo = new NinjaMailerObject;
|
||||||
try {
|
$nmo->mailable = new NinjaMailer($mail_obj);
|
||||||
Mail::to($company_user->user->email)
|
$nmo->company = $this->companyl;
|
||||||
->send(new EntityNotificationMailer($mail_obj));
|
$nmo->to_user = $company_user->user;
|
||||||
} catch (\Exception $e) {
|
$nmo->settings = $this->settings;
|
||||||
//$this->failed($e);
|
|
||||||
$this->logMailError($e->getMessage(), $this->client);
|
NinjaMailerJob::dispatch($nmo);
|
||||||
}
|
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user