invoiceninja/app/Jobs/Mail/BaseMailerJob.php

121 lines
3.5 KiB
PHP
Raw Normal View History

<?php
2020-08-06 13:04:09 +10:00
/**
* Invoice Ninja (https://invoiceninja.com).
2020-08-06 13:04:09 +10:00
*
* @link https://github.com/invoiceninja/invoiceninja source repository
*
* @copyright Copyright (c) 2021. Invoice Ninja LLC (https://invoiceninja.com)
2020-08-06 13:04:09 +10:00
*
* @license https://opensource.org/licenses/AAL
*/
namespace App\Jobs\Mail;
use App\DataMapper\Analytics\EmailFailure;
2020-11-12 15:37:50 +11:00
use App\Jobs\Util\SystemLogger;
use App\Libraries\Google\Google;
2020-11-12 15:37:50 +11:00
use App\Models\SystemLog;
use App\Models\User;
use App\Providers\MailServiceProvider;
2020-11-10 15:06:46 +11:00
use App\Utils\Ninja;
2021-02-10 21:49:12 +11:00
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;
2020-11-10 15:06:46 +11:00
use Illuminate\Support\Facades\App;
use Illuminate\Support\Facades\Config;
2020-11-10 15:06:46 +11:00
use Illuminate\Support\Facades\Lang;
use Turbo124\Beacon\Facades\LightLogs;
2021-02-14 20:55:04 +11:00
/*
Multi Mailer implemented
@Deprecated 14/02/2021
*/
2020-08-06 13:04:09 +10:00
class BaseMailerJob implements ShouldQueue
{
2021-02-10 21:49:12 +11:00
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels, MakesHash;
2020-11-22 22:14:49 +11:00
public $tries = 5; //number of retries
public $backoff = 5; //seconds to wait until retry
public $deleteWhenMissingModels = true;
2020-08-10 14:55:44 +10:00
public function setMailDriver()
{
/* Singletons need to be rebooted each time just in case our Locale is changing*/
2020-11-10 15:06:46 +11:00
App::forgetInstance('translator');
2021-02-11 10:38:42 +11:00
App::forgetInstance('mail.manager'); //singletons must be destroyed!
/* Inject custom translations if any exist */
2020-11-10 15:06:46 +11:00
Lang::replace(Ninja::transformTranslations($this->settings));
2020-08-10 14:55:44 +10:00
switch ($this->settings->email_sending_method) {
case 'default':
break;
case 'gmail':
$this->setGmailMailer();
break;
default:
break;
}
}
public function setGmailMailer()
{
2020-08-10 14:55:44 +10:00
$sending_user = $this->settings->gmail_sending_user_id;
2021-02-10 21:49:12 +11:00
$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);
}
/*
2020-11-01 14:19:03 +11:00
* 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();
2021-02-11 22:37:58 +11:00
nlog("after registering mail service provider");
nlog(config('services.gmail.token'));
}
public function logMailError($errors, $recipient_object)
2020-11-12 15:37:50 +11:00
{
SystemLogger::dispatch(
$errors,
SystemLog::CATEGORY_MAIL,
SystemLog::EVENT_MAIL_SEND,
SystemLog::TYPE_FAILURE,
$recipient_object
);
}
public function failed($exception = null)
{
nlog('mailer job failed');
2020-12-30 08:10:03 +11:00
nlog($exception->getMessage());
2020-12-21 12:52:33 +11:00
$job_failure = new EmailFailure();
$job_failure->string_metric5 = get_parent_class($this);
$job_failure->string_metric6 = $exception->getMessage();
LightLogs::create($job_failure)
->batch();
}
}