diff --git a/app/Http/Controllers/InvoiceController.php b/app/Http/Controllers/InvoiceController.php index 460c018811e3..a5e99de0b00e 100644 --- a/app/Http/Controllers/InvoiceController.php +++ b/app/Http/Controllers/InvoiceController.php @@ -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); } diff --git a/app/Http/Controllers/QuoteController.php b/app/Http/Controllers/QuoteController.php index 7f6cc661bc3b..5b777216336a 100644 --- a/app/Http/Controllers/QuoteController.php +++ b/app/Http/Controllers/QuoteController.php @@ -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); } diff --git a/app/Http/Controllers/UserController.php b/app/Http/Controllers/UserController.php index bc84f38f12e3..0f8cfd3c58d4 100644 --- a/app/Http/Controllers/UserController.php +++ b/app/Http/Controllers/UserController.php @@ -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 diff --git a/app/Jobs/Entity/EmailEntity.php b/app/Jobs/Entity/EmailEntity.php index b4d8ca973748..95c16acbf030 100644 --- a/app/Jobs/Entity/EmailEntity.php +++ b/app/Jobs/Entity/EmailEntity.php @@ -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 */ diff --git a/app/Jobs/Invoice/ZipInvoices.php b/app/Jobs/Invoice/ZipInvoices.php index 01fb041a7cb3..4d1035db9375 100644 --- a/app/Jobs/Invoice/ZipInvoices.php +++ b/app/Jobs/Invoice/ZipInvoices.php @@ -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)); } diff --git a/app/Jobs/Mail/NinjaMailerJob.php b/app/Jobs/Mail/NinjaMailerJob.php index 2f858564a99e..d1b4134bfc54 100644 --- a/app/Jobs/Mail/NinjaMailerJob.php +++ b/app/Jobs/Mail/NinjaMailerJob.php @@ -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() diff --git a/app/Jobs/Mail/NinjaMailerObject.php b/app/Jobs/Mail/NinjaMailerObject.php index 0d8007608b83..ec34c5f39236 100644 --- a/app/Jobs/Mail/NinjaMailerObject.php +++ b/app/Jobs/Mail/NinjaMailerObject.php @@ -35,4 +35,7 @@ class NinjaMailerObject public $invitation = FALSE; public $template = FALSE; + + public $entity = FALSE; + } diff --git a/app/Jobs/Payment/EmailPayment.php b/app/Jobs/Payment/EmailPayment.php index 2530636ee993..134b0673eac6 100644 --- a/app/Jobs/Payment/EmailPayment.php +++ b/app/Jobs/Payment/EmailPayment.php @@ -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())); } diff --git a/app/Jobs/User/UserEmailChanged.php b/app/Jobs/User/UserEmailChanged.php index 425df5481e9e..b4021fe1cb6a 100644 --- a/app/Jobs/User/UserEmailChanged.php +++ b/app/Jobs/User/UserEmailChanged.php @@ -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() diff --git a/app/Mail/DownloadInvoices.php b/app/Mail/DownloadInvoices.php index 70237def6ec0..006703f1f382 100644 --- a/app/Mail/DownloadInvoices.php +++ b/app/Mail/DownloadInvoices.php @@ -24,9 +24,6 @@ class DownloadInvoices extends Mailable /** * Build the message. - * - * @return $this - * @throws \Laracasts\Presenter\Exceptions\PresenterException */ public function build() {