2020-05-16 04:04:24 +02:00
|
|
|
<?php
|
2020-08-06 05:04:09 +02:00
|
|
|
/**
|
2020-09-06 11:38:10 +02:00
|
|
|
* Invoice Ninja (https://invoiceninja.com).
|
2020-08-06 05:04:09 +02:00
|
|
|
*
|
|
|
|
* @link https://github.com/invoiceninja/invoiceninja source repository
|
|
|
|
*
|
|
|
|
* @copyright Copyright (c) 2020. Invoice Ninja LLC (https://invoiceninja.com)
|
|
|
|
*
|
|
|
|
* @license https://opensource.org/licenses/AAL
|
|
|
|
*/
|
2020-05-16 04:04:24 +02:00
|
|
|
|
|
|
|
namespace App\Jobs\Mail;
|
|
|
|
|
2020-10-10 05:32:10 +02:00
|
|
|
use App\DataMapper\Analytics\EmailFailure;
|
2020-11-12 05:37:50 +01:00
|
|
|
use App\Jobs\Util\SystemLogger;
|
2020-05-16 04:04:24 +02:00
|
|
|
use App\Libraries\Google\Google;
|
2020-11-12 05:37:50 +01:00
|
|
|
use App\Models\SystemLog;
|
2020-05-16 04:04:24 +02:00
|
|
|
use App\Models\User;
|
|
|
|
use App\Providers\MailServiceProvider;
|
2020-11-10 05:06:46 +01:00
|
|
|
use App\Utils\Ninja;
|
2020-05-16 04:04:24 +02:00
|
|
|
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 05:06:46 +01:00
|
|
|
use Illuminate\Support\Facades\App;
|
2020-05-16 04:04:24 +02:00
|
|
|
use Illuminate\Support\Facades\Config;
|
2020-11-10 05:06:46 +01:00
|
|
|
use Illuminate\Support\Facades\Lang;
|
2020-10-10 05:32:10 +02:00
|
|
|
use Turbo124\Beacon\Facades\LightLogs;
|
2020-05-16 04:04:24 +02:00
|
|
|
|
2020-08-06 05:04:09 +02:00
|
|
|
/*Multi Mailer implemented*/
|
|
|
|
|
2020-05-16 04:04:24 +02:00
|
|
|
class BaseMailerJob implements ShouldQueue
|
|
|
|
{
|
|
|
|
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
|
|
|
|
|
2020-11-22 12:14:49 +01:00
|
|
|
public $tries = 5; //number of retries
|
|
|
|
|
|
|
|
public $backoff = 5; //seconds to wait until retry
|
|
|
|
|
|
|
|
public $deleteWhenMissingModels = true;
|
|
|
|
|
2020-08-10 06:55:44 +02:00
|
|
|
public function setMailDriver()
|
2020-05-16 04:04:24 +02:00
|
|
|
{
|
2020-11-10 05:06:46 +01:00
|
|
|
App::forgetInstance('translator');
|
|
|
|
Lang::replace(Ninja::transformTranslations($this->settings));
|
|
|
|
|
2020-08-10 06:55:44 +02:00
|
|
|
switch ($this->settings->email_sending_method) {
|
2020-05-16 04:04:24 +02:00
|
|
|
case 'default':
|
|
|
|
break;
|
|
|
|
case 'gmail':
|
|
|
|
$this->setGmailMailer();
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-16 12:26:16 +02:00
|
|
|
public function setGmailMailer()
|
2020-05-16 04:04:24 +02:00
|
|
|
{
|
2020-08-10 06:55:44 +02:00
|
|
|
$sending_user = $this->settings->gmail_sending_user_id;
|
2020-05-16 04:04:24 +02:00
|
|
|
|
|
|
|
$user = User::find($sending_user);
|
|
|
|
|
|
|
|
$google = (new Google())->init();
|
2020-05-16 13:13:32 +02:00
|
|
|
$google->getClient()->setAccessToken(json_encode($user->oauth_user_token));
|
2020-05-16 04:04:24 +02:00
|
|
|
|
2020-05-16 12:26:16 +02:00
|
|
|
if ($google->getClient()->isAccessTokenExpired()) {
|
2020-05-16 04:04:24 +02:00
|
|
|
$google->refreshToken($user);
|
|
|
|
}
|
|
|
|
|
2020-09-06 11:38:10 +02:00
|
|
|
/*
|
2020-11-01 04:19:03 +01:00
|
|
|
* Now that our token is refreshed and valid we can boot the
|
2020-05-16 04:04:24 +02:00
|
|
|
* mail driver at runtime and also set the token which will persist
|
|
|
|
* just for this request.
|
|
|
|
*/
|
2020-09-06 11:38:10 +02:00
|
|
|
|
2020-05-16 04:04:24 +02:00
|
|
|
Config::set('mail.driver', 'gmail');
|
2020-05-16 13:13:32 +02:00
|
|
|
Config::set('services.gmail.token', $user->oauth_user_token->access_token);
|
2020-12-02 04:08:35 +01:00
|
|
|
Config::set('mail.from.address', $user->email);
|
|
|
|
Config::set('mail.from.name', $user->present()->name());
|
2020-05-16 12:26:16 +02:00
|
|
|
|
2020-05-16 04:04:24 +02:00
|
|
|
(new MailServiceProvider(app()))->register();
|
|
|
|
}
|
|
|
|
|
2020-08-08 01:28:36 +02:00
|
|
|
public function logMailError($errors, $recipient_object)
|
2020-11-12 05:37:50 +01:00
|
|
|
{
|
2020-08-08 01:28:36 +02:00
|
|
|
SystemLogger::dispatch(
|
|
|
|
$errors,
|
|
|
|
SystemLog::CATEGORY_MAIL,
|
|
|
|
SystemLog::EVENT_MAIL_SEND,
|
|
|
|
SystemLog::TYPE_FAILURE,
|
|
|
|
$recipient_object
|
|
|
|
);
|
|
|
|
}
|
2020-10-10 05:32:10 +02:00
|
|
|
|
|
|
|
public function failed($exception = null)
|
|
|
|
{
|
|
|
|
|
2020-10-23 06:18:16 +02:00
|
|
|
// info('the job failed');
|
2020-10-10 05:32:10 +02:00
|
|
|
|
|
|
|
$job_failure = new EmailFailure();
|
|
|
|
$job_failure->string_metric5 = get_parent_class($this);
|
|
|
|
$job_failure->string_metric6 = $exception->getMessage();
|
|
|
|
|
|
|
|
LightLogs::create($job_failure)
|
|
|
|
->batch();
|
|
|
|
}
|
2020-09-06 11:38:10 +02:00
|
|
|
}
|