1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-11-10 13:12:50 +01:00

Refactoring emails

This commit is contained in:
David Bomba 2021-02-18 10:30:31 +11:00
parent ac0332eacc
commit 06bc49b0d6
10 changed files with 74 additions and 69 deletions

View File

@ -532,7 +532,7 @@ class InvoiceController extends BaseController
}
});
ZipInvoices::dispatch($invoices, $invoices->first()->company, auth()->user()->email);
ZipInvoices::dispatch($invoices, $invoices->first()->company, auth()->user());
return response()->json(['message' => ctrans('texts.sent_message')], 200);
}

View File

@ -524,7 +524,7 @@ class QuoteController extends BaseController
}
});
ZipInvoices::dispatch($quotes, $quotes->first()->company, auth()->user()->email);
ZipInvoices::dispatch($quotes, $quotes->first()->company, auth()->user());
return response()->json(['message' => ctrans('texts.sent_message')], 200);
}

View File

@ -369,7 +369,6 @@ class UserController extends BaseController
*/
public function update(UpdateUserRequest $request, User $user)
{
$old_email = $user->email;
$old_company_user = $user->company_user;
$old_user = $user;
@ -378,10 +377,9 @@ class UserController extends BaseController
$user = $this->user_repo->save($request->all(), $user);
$user = $user->fresh();
if ($old_email != $new_email) {
UserEmailChanged::dispatch($new_email, $old_email, auth()->user()->company());
}
if ($old_user->email != $new_email)
UserEmailChanged::dispatch($new_user, $old_user, auth()->user()->company());
if(
strcasecmp($old_company_user->permissions, $user->company_user->permissions) != 0 ||
$old_company_user->is_admin != $user->company_user->is_admin

View File

@ -14,7 +14,6 @@ namespace App\Jobs\Entity;
use App\Events\Invoice\InvoiceReminderWasEmailed;
use App\Events\Invoice\InvoiceWasEmailed;
use App\Events\Invoice\InvoiceWasEmailedAndFailed;
use App\Jobs\Mail\BaseMailerJob;
use App\Jobs\Mail\EntityFailedSendMailer;
use App\Jobs\Mail\NinjaMailerJob;
use App\Jobs\Mail\NinjaMailerObject;
@ -113,7 +112,8 @@ class EmailEntity implements ShouldQueue
$nmo->entity_string = $this->entity_string;
$nmo->invitation = $this->invitation;
$nmo->reminder_template = $this->reminder_template;
$nmo->entity = $this->entity;
NinjaMailerJob::dispatch($nmo);
/* Mark entity sent */

View File

@ -11,10 +11,12 @@
namespace App\Jobs\Invoice;
use App\Jobs\Mail\BaseMailerJob;
use App\Jobs\Mail\NinjaMailerJob;
use App\Jobs\Mail\NinjaMailerObject;
use App\Jobs\Util\UnlinkFile;
use App\Mail\DownloadInvoices;
use App\Models\Company;
use App\Models\User;
use App\Utils\TempFile;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
@ -26,7 +28,7 @@ use Illuminate\Support\Facades\Storage;
use ZipStream\Option\Archive;
use ZipStream\ZipStream;
class ZipInvoices extends BaseMailerJob implements ShouldQueue
class ZipInvoices implements ShouldQueue
{
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
@ -34,7 +36,7 @@ class ZipInvoices extends BaseMailerJob implements ShouldQueue
private $company;
private $email;
private $user;
public $settings;
@ -46,13 +48,13 @@ class ZipInvoices extends BaseMailerJob implements ShouldQueue
* Create a new job instance.
*
*/
public function __construct($invoices, Company $company, $email)
public function __construct($invoices, Company $company, User $user)
{
$this->invoices = $invoices;
$this->company = $company;
$this->email = $email;
$this->user = $user;
$this->settings = $company->settings;
}
@ -90,14 +92,13 @@ class ZipInvoices extends BaseMailerJob implements ShouldQueue
fclose($tempStream);
$this->setMailDriver();
try {
Mail::to($this->email)
->send(new DownloadInvoices(Storage::disk(config('filesystems.default'))->url($path.$file_name), $this->company));
} catch (\Exception $e) {
// //$this->failed($e);
}
$nmo = new NinjaMailerObject;
$nmo->mailable = new DownloadInvoices(Storage::disk(config('filesystems.default'))->url($path.$file_name), $this->company);
$nmo->to_user = $this->user;
$nmo->settings = $this->settings;
$nmo->company = $this->company;
NinjaMailerJob::dispatch($nmo);
UnlinkFile::dispatch(config('filesystems.default'), $path.$file_name)->delay(now()->addHours(1));
}

View File

@ -13,17 +13,21 @@ namespace App\Jobs\Mail;
use App\DataMapper\Analytics\EmailFailure;
use App\Events\Invoice\InvoiceWasEmailedAndFailed;
use App\Events\Payment\PaymentWasEmailedAndFailed;
use App\Jobs\Mail\NinjaMailerObject;
use App\Jobs\Util\SystemLogger;
use App\Libraries\Google\Google;
use App\Libraries\MultiDB;
use App\Mail\TemplateEmail;
use App\Models\ClientContact;
use App\Models\Invoice;
use App\Models\Payment;
use App\Models\SystemLog;
use App\Models\User;
use App\Providers\MailServiceProvider;
use App\Utils\Ninja;
use App\Utils\Traits\MakesHash;
use Dacastro4\LaravelGmail\Facade\LaravelGmail;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
@ -34,7 +38,6 @@ use Illuminate\Support\Facades\Config;
use Illuminate\Support\Facades\Lang;
use Illuminate\Support\Facades\Mail;
use Turbo124\Beacon\Facades\LightLogs;
use Dacastro4\LaravelGmail\Facade\LaravelGmail;
/*Multi Mailer implemented*/
@ -78,10 +81,7 @@ class NinjaMailerJob implements ShouldQueue
nlog("error failed with {$e->getMessage()}");
if ($this->nmo->to_user instanceof ClientContact)
$this->logMailError($e->getMessage(), $this->nmo->to_user->client);
if($this->nmo->entity_string)
if($this->nmo->entity)
$this->entityEmailFailed($e->getMessage());
}
}
@ -89,15 +89,22 @@ class NinjaMailerJob implements ShouldQueue
/* Switch statement to handle failure notifications */
private function entityEmailFailed($message)
{
switch ($this->nmo->entity_string) {
case 'invoice':
$class = get_class($this->nmo->entity);
switch ($class) {
case Invoice::class:
event(new InvoiceWasEmailedAndFailed($this->nmo->invitation, $this->nmo->company, $message, $this->nmo->reminder_template, Ninja::eventVars()));
break;
case Payment::class:
event(new PaymentWasEmailedAndFailed($this->nmo->entity, $this->nmo->company, $message, Ninja::eventVars()));
break;
default:
# code...
break;
}
if ($this->nmo->to_user instanceof ClientContact)
$this->logMailError($e->getMessage(), $this->nmo->to_user->client);
}
private function setMailDriver()

View File

@ -35,4 +35,7 @@ class NinjaMailerObject
public $invitation = FALSE;
public $template = FALSE;
public $entity = FALSE;
}

View File

@ -13,7 +13,8 @@ namespace App\Jobs\Payment;
use App\Events\Payment\PaymentWasEmailed;
use App\Events\Payment\PaymentWasEmailedAndFailed;
use App\Jobs\Mail\BaseMailerJob;
use App\Jobs\Mail\NinjaMailerJob;
use App\Jobs\Mail\NinjaMailerObject;
use App\Libraries\MultiDB;
use App\Mail\Engine\PaymentEmailEngine;
use App\Mail\TemplateEmail;
@ -28,7 +29,7 @@ use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
use Illuminate\Support\Facades\Mail;
class EmailPayment extends BaseMailerJob implements ShouldQueue
class EmailPayment implements ShouldQueue
{
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
@ -66,27 +67,24 @@ class EmailPayment extends BaseMailerJob implements ShouldQueue
*/
public function handle()
{
if ($this->company->is_disabled) {
if ($this->company->is_disabled)
return true;
}
if ($this->contact->email) {
MultiDB::setDb($this->company->db);
//if we need to set an email driver do it now
$this->setMailDriver();
MultiDB::setDb($this->company->db);
$email_builder = (new PaymentEmailEngine($this->payment, $this->contact))->build();
try {
$mail = Mail::to($this->contact->email, $this->contact->present()->name());
$mail->send(new TemplateEmail($email_builder, $this->contact));
} catch (\Exception $e) {
nlog("mailing failed with message " . $e->getMessage());
event(new PaymentWasEmailedAndFailed($this->payment, $this->company, Mail::failures(), Ninja::eventVars()));
//$this->failed($e);
return $this->logMailError($e->getMessage(), $this->payment->client);
}
$nmo = new NinjaMailerObject;
$nmo->mailable = new TemplateEmail($email_builder, $this->contact);
$nmo->to_user = $this->contact;
$nmo->settings = $this->settings;
$nmo->company = $this->company;
$nmo->entity = $this->payment;
NinjaMailerJob::dispatch($nmo);
event(new PaymentWasEmailed($this->payment, $this->payment->company, Ninja::eventVars()));
}

View File

@ -11,10 +11,12 @@
namespace App\Jobs\User;
use App\Jobs\Mail\BaseMailerJob;
use App\Jobs\Mail\NinjaMailerJob;
use App\Jobs\Mail\NinjaMailerObject;
use App\Libraries\MultiDB;
use App\Mail\User\UserNotificationMailer;
use App\Models\Company;
use App\Models\User;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
@ -23,13 +25,13 @@ use Illuminate\Queue\SerializesModels;
use Illuminate\Support\Facades\Mail;
use stdClass;
class UserEmailChanged extends BaseMailerJob implements ShouldQueue
class UserEmailChanged implements ShouldQueue
{
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
protected $new_email;
protected $new_user;
protected $old_email;
protected $old_user;
protected $company;
@ -42,10 +44,10 @@ class UserEmailChanged extends BaseMailerJob implements ShouldQueue
* @param string $old_email
* @param Company $company
*/
public function __construct(string $new_email, string $old_email, Company $company)
public function __construct(User $new_user, User $old_user, Company $company)
{
$this->new_email = $new_email;
$this->old_email = $old_email;
$this->new_user = $new_user;
$this->old_user = $old_user;
$this->company = $company;
$this->settings = $this->company->settings;
}
@ -59,9 +61,6 @@ class UserEmailChanged extends BaseMailerJob implements ShouldQueue
//Set DB
MultiDB::setDb($this->company->db);
//If we need to set an email driver do it now
$this->setMailDriver();
/*Build the object*/
$mail_obj = new stdClass;
$mail_obj->subject = ctrans('texts.email_address_changed');
@ -71,17 +70,19 @@ class UserEmailChanged extends BaseMailerJob implements ShouldQueue
$mail_obj->data = $this->getData();
//Send email via a Mailable class
//
try {
Mail::to($this->old_email)
->send(new UserNotificationMailer($mail_obj));
$nmo = new NinjaMailerObject;
$nmo->mailable = new UserNotificationMailer($mail_obj);
$nmo->settings = $this->settings;
$nmo->company = $this->company;
$nmo->to_user = $this->old_user;
NinjaMailerJob::dispatch($nmo);
$nmo->to_user = $this->new_user;
NinjaMailerJob::dispatch($nmo);
Mail::to($this->new_email)
->send(new UserNotificationMailer($mail_obj));
} catch (\Exception $e) {
//$this->failed($e);
$this->logMailError($e->getMessage(), $this->company->owner());
}
}
private function getData()

View File

@ -24,9 +24,6 @@ class DownloadInvoices extends Mailable
/**
* Build the message.
*
* @return $this
* @throws \Laracasts\Presenter\Exceptions\PresenterException
*/
public function build()
{