From dea73bcdf87e3903abde71936a135caa4fb35f16 Mon Sep 17 00:00:00 2001 From: yogesh-gohil Date: Fri, 17 Mar 2023 18:54:58 +0530 Subject: [PATCH] Refactor mail-sender --- .../Admin/MailSender/MailSenderController.php | 21 ++--------- .../V1/Customer/EstimatePdfController.php | 17 +++++---- .../V1/Customer/InvoicePdfController.php | 13 ++++--- app/Http/Middleware/ConfigMiddleware.php | 7 ++++ app/Mail/EstimateViewedMail.php | 2 +- app/Mail/InvoiceViewedMail.php | 2 +- app/Models/MailSender.php | 17 +++++++++ app/Space/helpers.php | 37 +++++++++++++++++++ app/Traits/MailTrait.php | 32 +++------------- .../dropdowns/MailSenderIndexDropdown.vue | 18 ++++----- 10 files changed, 99 insertions(+), 67 deletions(-) diff --git a/app/Http/Controllers/V1/Admin/MailSender/MailSenderController.php b/app/Http/Controllers/V1/Admin/MailSender/MailSenderController.php index 697a660d..38f44c32 100644 --- a/app/Http/Controllers/V1/Admin/MailSender/MailSenderController.php +++ b/app/Http/Controllers/V1/Admin/MailSender/MailSenderController.php @@ -41,14 +41,6 @@ class MailSenderController extends Controller { $this->authorize('create', MailSender::class); - $mailConfiguration = MailSender::where('company_id', $request->header('company')) - ->where('is_default', true) - ->first(); - - if ($mailConfiguration && $request['is_default'] == true) { - $mailConfiguration->update(['is_default' => false]); - } - $mailSender = MailSender::createFromRequest($request); return new MailSenderResource($mailSender); @@ -78,15 +70,6 @@ class MailSenderController extends Controller { $this->authorize('update', $mailSender); - $mailConfiguration = MailSender::where('company_id', $request->header('company')) - ->where('is_default', true) - ->where('id', '<>', $mailSender->id) - ->first(); - - if ($mailConfiguration && $request['is_default'] == true) { - $mailConfiguration->update(['is_default' => false]); - } - $mailSender->updateFromRequest($request); return new MailSenderResource($mailSender); @@ -102,6 +85,10 @@ class MailSenderController extends Controller { $this->authorize('delete', $mailSender); + if ($mailSender->is_default) { + return respondJson('You can\'t remove default mail sender.', 'You can\'t remove default mail sender.'); + } + $mailSender->delete(); return response()->json([ diff --git a/app/Http/Controllers/V1/Customer/EstimatePdfController.php b/app/Http/Controllers/V1/Customer/EstimatePdfController.php index 9cba60ce..dfa0d08c 100644 --- a/app/Http/Controllers/V1/Customer/EstimatePdfController.php +++ b/app/Http/Controllers/V1/Customer/EstimatePdfController.php @@ -9,6 +9,7 @@ use Crater\Models\CompanySetting; use Crater\Models\Customer; use Crater\Models\EmailLog; use Crater\Models\Estimate; +use Crater\Models\MailSender; use Illuminate\Http\Request; class EstimatePdfController extends Controller @@ -27,14 +28,16 @@ class EstimatePdfController extends Controller ); if ($notifyEstimateViewed == 'YES') { - $data['estimate'] = Estimate::findOrFail($estimate->id)->toArray(); - $data['user'] = Customer::find($estimate->customer_id)->toArray(); - $notificationEmail = CompanySetting::getSetting( - 'notification_email', - $estimate->company_id - ); + $notificationEmail = CompanySetting::getSetting('notification_email', $estimate->company_id); + $mailSender = MailSender::where('company_id', $estimate->company_id)->where('is_default', true)->first(); + MailSender::setMailConfiguration($mailSender->id); - \Mail::to($notificationEmail)->send(new EstimateViewedMail($data)); + $data['from_address'] = $mailSender->from_address; + $data['from_name'] = $mailSender->from_name; + $data['user'] = Customer::find($estimate->customer_id)->toArray(); + $data['estimate'] = Estimate::findOrFail($estimate->id)->toArray(); + + send_mail(new EstimateViewedMail($data), $mailSender, $notificationEmail); } } diff --git a/app/Http/Controllers/V1/Customer/InvoicePdfController.php b/app/Http/Controllers/V1/Customer/InvoicePdfController.php index ad94403c..ee63de95 100644 --- a/app/Http/Controllers/V1/Customer/InvoicePdfController.php +++ b/app/Http/Controllers/V1/Customer/InvoicePdfController.php @@ -9,6 +9,7 @@ use Crater\Models\CompanySetting; use Crater\Models\Customer; use Crater\Models\EmailLog; use Crater\Models\Invoice; +use Crater\Models\MailSender; use Illuminate\Http\Request; class InvoicePdfController extends Controller @@ -28,14 +29,16 @@ class InvoicePdfController extends Controller ); if ($notifyInvoiceViewed == 'YES') { + $notificationEmail = CompanySetting::getSetting('notification_email', $invoice->company_id); + $mailSender = MailSender::where('company_id', $invoice->company_id)->where('is_default', true)->first(); + MailSender::setMailConfiguration($mailSender->id); + + $data['from_address'] = $mailSender->from_address; + $data['from_name'] = $mailSender->from_name; $data['invoice'] = Invoice::findOrFail($invoice->id)->toArray(); $data['user'] = Customer::find($invoice->customer_id)->toArray(); - $notificationEmail = CompanySetting::getSetting( - 'notification_email', - $invoice->company_id - ); - \Mail::to($notificationEmail)->send(new InvoiceViewedMail($data)); + send_mail(new InvoiceViewedMail($data), $mailSender, $notificationEmail); } } diff --git a/app/Http/Middleware/ConfigMiddleware.php b/app/Http/Middleware/ConfigMiddleware.php index 2bcfc07f..948b7d36 100644 --- a/app/Http/Middleware/ConfigMiddleware.php +++ b/app/Http/Middleware/ConfigMiddleware.php @@ -4,6 +4,7 @@ namespace Crater\Http\Middleware; use Closure; use Crater\Models\FileDisk; +use Crater\Models\MailSender; class ConfigMiddleware { @@ -28,6 +29,12 @@ class ConfigMiddleware } } + $default_mail_sender = MailSender::where('company_id', $request->header('company'))->where('is_default', true)->first(); + + if ($default_mail_sender) { + $default_mail_sender->setMailConfiguration($default_mail_sender->id); + } + return $next($request); } } diff --git a/app/Mail/EstimateViewedMail.php b/app/Mail/EstimateViewedMail.php index a29d8f6c..61b4f1cd 100644 --- a/app/Mail/EstimateViewedMail.php +++ b/app/Mail/EstimateViewedMail.php @@ -30,7 +30,7 @@ class EstimateViewedMail extends Mailable */ public function build() { - return $this->from(config('mail.from.address'), config('mail.from.name')) + return $this->from($this->data['from_address'], $this->data['from_name']) ->markdown('emails.viewed.estimate', ['data', $this->data]); } } diff --git a/app/Mail/InvoiceViewedMail.php b/app/Mail/InvoiceViewedMail.php index 94e858b3..d63a01ff 100644 --- a/app/Mail/InvoiceViewedMail.php +++ b/app/Mail/InvoiceViewedMail.php @@ -30,7 +30,7 @@ class InvoiceViewedMail extends Mailable */ public function build() { - return $this->from(config('mail.from.address'), config('mail.from.name')) + return $this->from($this->data['from_address'], $this->data['from_name']) ->markdown('emails.viewed.invoice', ['data', $this->data]); } } diff --git a/app/Models/MailSender.php b/app/Models/MailSender.php index 1dedd9ab..bc286013 100644 --- a/app/Models/MailSender.php +++ b/app/Models/MailSender.php @@ -59,6 +59,10 @@ class MailSender extends Model { $senderMail = self::create($request->getMailSenderPayload()); + if ($request->is_default) { + $senderMail->removeOtherDefaultMailSenders($request); + } + return $senderMail; } @@ -68,6 +72,10 @@ class MailSender extends Model $this->update($data); + if ($request->is_default) { + $this->removeOtherDefaultMailSenders($request); + } + return $this; } @@ -81,6 +89,9 @@ class MailSender extends Model 'address' => $mailSender->from_address, 'name' => $mailSender->from_name ]; + $settings['sendmail'] = config('mail.sendmail'); + $settings['markdown'] = config('mail.markdown'); + $settings['log_channel'] = config('mail.log_channel'); Config::set('mail', $settings); @@ -91,4 +102,10 @@ class MailSender extends Model return true; } + public function removeOtherDefaultMailSenders($request) { + MailSender::where('company_id', $request->header('company')) + ->where('is_default', true) + ->where('id', '<>', $this->id) + ->update(['is_default' => false]); + } } diff --git a/app/Space/helpers.php b/app/Space/helpers.php index 8e14fe0a..877c25c1 100644 --- a/app/Space/helpers.php +++ b/app/Space/helpers.php @@ -5,6 +5,7 @@ use Crater\Models\Currency; use Crater\Models\CustomField; use Crater\Models\Setting; use Illuminate\Support\Str; +use Illuminate\Mail\Mailable; /** * Get company setting @@ -70,6 +71,42 @@ function set_active($path, $active = 'active') return call_user_func_array('Request::is', (array)$path) ? $active : ''; } +/** + * Send Mail + * + * @param Mailable $mailable + * @param object $mailSender + * @return string $to + */ +function send_mail(Mailable $mailable, object $mailSender = null, string $to) +{ + if ($mailSender->bcc && $mailSender->cc) { + \Mail::to($to) + ->bcc(explode(',', $mailSender->bcc)) + ->cc(explode(',', $mailSender->cc)) + ->send($mailable); + } + + if ($mailSender->bcc && $mailSender->cc == null) { + \Mail::to($to) + ->bcc(explode(',', $mailSender->bcc)) + ->send($mailable); + } + + if ($mailSender->bcc == null && $mailSender->cc) { + \Mail::to($to) + ->cc(explode(',', $mailSender->cc)) + ->send($mailable); + } + + if ($mailSender->bcc == null && $mailSender->cc == null) { + \Mail::to($to) + ->send($mailable); + } + + return true; +} + /** * @param $path * @return mixed diff --git a/app/Traits/MailTrait.php b/app/Traits/MailTrait.php index 54ca6062..e940bc20 100644 --- a/app/Traits/MailTrait.php +++ b/app/Traits/MailTrait.php @@ -2,6 +2,8 @@ namespace Crater\Traits; +use Crater\Mail\EstimateViewedMail; +use Crater\Mail\InvoiceViewedMail; use Crater\Mail\SendEstimateMail; use Crater\Mail\SendInvoiceMail; use Crater\Mail\SendPaymentMail; @@ -18,45 +20,21 @@ trait MailTrait switch ($model) { case 'invoice': - $mail = new SendInvoiceMail($data); + send_mail(new SendInvoiceMail($data), $mailSender, $data['to']); break; case 'estimate': - $mail = new SendEstimateMail($data); + send_mail(new SendEstimateMail($data), $mailSender, $data['to']); break; case 'payment': - $mail = new SendPaymentMail($data); + send_mail(new SendPaymentMail($data), $mailSender, $data['to']); break; } - if ($mailSender->bcc && $mailSender->cc) { - \Mail::to($data['to']) - ->bcc(explode(',', $mailSender->bcc)) - ->cc(explode(',', $mailSender->cc)) - ->send($mail); - } - - if ($mailSender->bcc && $mailSender->cc == null) { - \Mail::to($data['to']) - ->bcc(explode(',', $mailSender->bcc)) - ->send($mail); - } - - if ($mailSender->bcc == null && $mailSender->cc) { - \Mail::to($data['to']) - ->cc(explode(',', $mailSender->cc)) - ->send($mail); - } - - if ($mailSender->bcc == null && $mailSender->cc == null) { - \Mail::to($data['to']) - ->send($mail); - } - return true; } } diff --git a/resources/scripts/admin/components/dropdowns/MailSenderIndexDropdown.vue b/resources/scripts/admin/components/dropdowns/MailSenderIndexDropdown.vue index 40111929..8b1226a7 100644 --- a/resources/scripts/admin/components/dropdowns/MailSenderIndexDropdown.vue +++ b/resources/scripts/admin/components/dropdowns/MailSenderIndexDropdown.vue @@ -16,15 +16,6 @@ {{ $t('general.edit') }} - - - - {{ $t('general.delete') }} - - {{ $t('general.send_test_mail') }} + + + + + {{ $t('general.delete') }} +