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

222 lines
6.8 KiB
PHP
Raw Normal View History

2021-02-14 11:43:44 +01:00
<?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;
2021-04-24 15:19:46 +02:00
use App\DataMapper\Analytics\EmailSuccess;
2021-02-16 13:56:12 +01:00
use App\Events\Invoice\InvoiceWasEmailedAndFailed;
2021-02-18 00:30:31 +01:00
use App\Events\Payment\PaymentWasEmailedAndFailed;
2021-02-14 11:43:44 +01:00
use App\Jobs\Mail\NinjaMailerObject;
use App\Jobs\Util\SystemLogger;
use App\Libraries\Google\Google;
use App\Libraries\MultiDB;
2021-02-16 13:56:12 +01:00
use App\Mail\TemplateEmail;
2021-02-14 11:43:44 +01:00
use App\Models\ClientContact;
2021-02-18 00:30:31 +01:00
use App\Models\Invoice;
use App\Models\Payment;
2021-02-14 11:43:44 +01:00
use App\Models\SystemLog;
use App\Models\User;
use App\Providers\MailServiceProvider;
use App\Utils\Ninja;
use App\Utils\Traits\MakesHash;
2021-02-18 00:30:31 +01:00
use Dacastro4\LaravelGmail\Facade\LaravelGmail;
2021-02-14 11:43:44 +01:00
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;
2021-02-16 13:56:12 +01:00
/*Set the correct database*/
2021-02-14 11:43:44 +01:00
MultiDB::setDb($this->nmo->company->db);
2021-02-16 13:56:12 +01:00
/* Set the email driver */
2021-02-14 11:43:44 +01:00
$this->setMailDriver();
2021-02-24 00:39:37 +01:00
if (strlen($this->nmo->settings->reply_to_email) > 1) {
2021-05-03 06:50:55 +02:00
if(property_exists($this->nmo->settings, 'reply_to_name'))
$reply_to_name = strlen($this->nmo->settings->reply_to_name) > 3 ? $this->nmo->settings->reply_to_name : $this->nmo->settings->reply_to_email;
else
$reply_to_name = $this->nmo->settings->reply_to_email;
2021-02-24 00:39:37 +01:00
$this->nmo->mailable->replyTo($this->nmo->settings->reply_to_email, $reply_to_name);
}
2021-05-08 02:57:18 +02:00
if (strlen($this->nmo->settings->bcc_email) > 1) {
2021-05-08 03:37:32 +02:00
nlog('bcc list available');
nlog($this->nmo->settings->bcc_email);
2021-05-08 02:57:18 +02:00
$this->nmo->mailable->bcc(explode(",", $this->nmo->settings->bcc_email), 'Blind Copy');
}
2021-02-24 00:43:08 +01:00
2021-02-24 00:39:37 +01:00
2021-02-14 11:43:44 +01:00
//send email
try {
2021-02-14 12:36:36 +01:00
nlog("trying to send");
2021-04-25 00:18:35 +02:00
2021-02-14 11:43:44 +01:00
Mail::to($this->nmo->to_user->email)
->send($this->nmo->mailable);
2021-04-25 00:18:35 +02:00
2021-04-25 00:33:25 +02:00
LightLogs::create(new EmailSuccess($this->nmo->company->company_key))
2021-04-25 00:18:35 +02:00
->batch();
2021-02-14 11:43:44 +01:00
} catch (\Exception $e) {
2021-02-16 13:56:12 +01:00
2021-02-14 12:36:36 +01:00
nlog("error failed with {$e->getMessage()}");
2021-03-04 09:42:22 +01:00
// nlog($e);
2021-02-16 13:56:12 +01:00
2021-02-18 00:30:31 +01:00
if($this->nmo->entity)
2021-02-16 13:56:12 +01:00
$this->entityEmailFailed($e->getMessage());
}
}
/* Switch statement to handle failure notifications */
private function entityEmailFailed($message)
{
2021-02-18 00:30:31 +01:00
$class = get_class($this->nmo->entity);
switch ($class) {
case Invoice::class:
2021-04-14 06:41:04 +02:00
event(new InvoiceWasEmailedAndFailed($this->nmo->invitation, $this->nmo->company, $message, $this->nmo->reminder_template, Ninja::eventVars(auth()->user() ? auth()->user()->id : null)));
2021-02-16 13:56:12 +01:00
break;
2021-02-18 00:30:31 +01:00
case Payment::class:
2021-04-14 07:28:05 +02:00
event(new PaymentWasEmailedAndFailed($this->nmo->entity, $this->nmo->company, $message, Ninja::eventVars(auth()->user() ? auth()->user()->id : null)));
2021-02-18 00:30:31 +01:00
break;
2021-02-16 13:56:12 +01:00
default:
# code...
break;
2021-02-14 11:43:44 +01:00
}
2021-02-18 00:30:31 +01:00
if ($this->nmo->to_user instanceof ClientContact)
2021-02-18 01:12:49 +01:00
$this->logMailError($message, $this->nmo->to_user->client);
2021-02-14 11:43:44 +01:00
}
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!
App::forgetInstance('mailer');
2021-02-17 01:25:30 +01:00
App::forgetInstance('laravelgmail');
2021-02-14 11:43:44 +01:00
/* Inject custom translations if any exist */
Lang::replace(Ninja::transformTranslations($this->nmo->settings));
switch ($this->nmo->settings->email_sending_method) {
case 'default':
2021-02-18 11:33:54 +01:00
//config(['mail.driver' => config('mail.default')]);
2021-02-14 11:43:44 +01:00
break;
case 'gmail':
$this->setGmailMailer();
break;
default:
break;
}
2021-02-17 01:25:30 +01:00
(new MailServiceProvider(app()))->register();
2021-02-14 11:43:44 +01:00
}
private function setGmailMailer()
{
2021-02-17 01:25:30 +01:00
if(LaravelGmail::check())
LaravelGmail::logout();
2021-02-16 11:59:49 +01:00
$sending_user = $this->nmo->settings->gmail_sending_user_id;
2021-02-14 11:43:44 +01:00
$user = User::find($this->decodePrimaryKey($sending_user));
2021-02-22 10:54:46 +01:00
nlog("Sending via {$user->name()}");
2021-02-16 12:19:07 +01:00
2021-02-14 11:43:44 +01:00
$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.
*/
2021-02-17 01:25:30 +01:00
config(['mail.driver' => 'gmail']);
(new MailServiceProvider(app()))->register();
$token = $user->oauth_user_token->access_token;
2021-02-17 01:25:30 +01:00
$this->nmo
->mailable
2021-02-22 10:54:46 +01:00
->from($user->email, $user->name())
->withSwiftMessage(function ($message) use($token) {
2021-02-17 01:25:30 +01:00
$message->getHeaders()->addTextHeader('GmailToken', $token);
});
2021-02-14 11:43:44 +01:00
}
private function logMailError($errors, $recipient_object)
{
SystemLogger::dispatch(
$errors,
SystemLog::CATEGORY_MAIL,
SystemLog::EVENT_MAIL_SEND,
SystemLog::TYPE_FAILURE,
$recipient_object
);
}
2021-02-16 11:59:49 +01:00
public function failed($exception = null)
2021-02-14 11:43:44 +01:00
{
nlog('mailer job failed');
nlog($exception->getMessage());
2021-04-24 15:19:46 +02:00
$job_failure = new EmailFailure($this->nmo->company->company_key);
2021-04-25 06:36:22 +02:00
$job_failure->string_metric5 = 'failed_email';
2021-04-25 02:56:33 +02:00
$job_failure->string_metric6 = substr($exception->getMessage(), 0, 150);
2021-02-14 11:43:44 +01:00
LightLogs::create($job_failure)
->batch();
}
}