1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-11-10 21:22:58 +01:00
invoiceninja/app/Jobs/Mail/BaseMailerJob.php

80 lines
2.1 KiB
PHP
Raw Normal View History

<?php
2020-08-06 05:04:09 +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
*/
namespace App\Jobs\Mail;
use App\Libraries\Google\Google;
use App\Libraries\MultiDB;
use App\Models\User;
use App\Providers\MailServiceProvider;
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\Config;
2020-08-06 05:04:09 +02:00
/*Multi Mailer implemented*/
class BaseMailerJob implements ShouldQueue
{
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
2020-08-10 06:55:44 +02:00
public function setMailDriver()
{
2020-08-10 06:55:44 +02:00
switch ($this->settings->email_sending_method) {
case 'default':
break;
case 'gmail':
$this->setGmailMailer();
break;
default:
break;
}
}
public function setGmailMailer()
{
2020-08-10 06:55:44 +02:00
$sending_user = $this->settings->gmail_sending_user_id;
$user = User::find($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 refresh and valid we can boot the
* mail driver at runtime and also set the token which will persist
* just for this request.
*/
Config::set('mail.driver', 'gmail');
Config::set('services.gmail.token', $user->oauth_user_token->access_token);
(new MailServiceProvider(app()))->register();
}
public function logMailError($errors, $recipient_object)
{
SystemLogger::dispatch(
$errors,
SystemLog::CATEGORY_MAIL,
SystemLog::EVENT_MAIL_SEND,
SystemLog::TYPE_FAILURE,
$recipient_object
);
}
}