From a97248555eb71bd0cab381003df8877cd705064e Mon Sep 17 00:00:00 2001 From: Hendrik Hagendorn Date: Sat, 27 May 2023 11:07:46 +0200 Subject: [PATCH] allow attaching uploaded files when sending mails --- app/Http/Requests/Common/CustomMail.php | 7 +- .../Document/SendDocumentAsCustomMail.php | 7 +- app/Notifications/Sale/Invoice.php | 21 +++- resources/lang/en-GB/general.php | 1 + .../views/modals/invoices/email.blade.php | 98 ++++++++++++++++--- 5 files changed, 118 insertions(+), 16 deletions(-) diff --git a/app/Http/Requests/Common/CustomMail.php b/app/Http/Requests/Common/CustomMail.php index ce449794b..24d3133d6 100644 --- a/app/Http/Requests/Common/CustomMail.php +++ b/app/Http/Requests/Common/CustomMail.php @@ -14,9 +14,10 @@ class CustomMail extends FormRequest public function rules() { return [ - 'to' => 'required|email', - 'subject' => 'required|string', - 'body' => 'required|string', + 'to' => 'required|email', + 'subject' => 'required|string', + 'body' => 'required|string', + 'attachments.*' => 'nullable|boolean', ]; } } diff --git a/app/Jobs/Document/SendDocumentAsCustomMail.php b/app/Jobs/Document/SendDocumentAsCustomMail.php index 986c69570..c5ca10fe7 100644 --- a/app/Jobs/Document/SendDocumentAsCustomMail.php +++ b/app/Jobs/Document/SendDocumentAsCustomMail.php @@ -30,10 +30,15 @@ class SendDocumentAsCustomMail extends Job $custom_mail['cc'] = user()->email; } + $attachments = collect($this->request->get('attachments', [])) + ->filter(fn($value) => $value == true) + ->keys() + ->all(); + $notification = config('type.document.' . $document->type . '.notification.class'); // Notify the contact - $document->contact->notify(new $notification($document, $this->template_alias, true, $custom_mail)); + $document->contact->notify(new $notification($document, $this->template_alias, true, $custom_mail, $attachments)); event(new DocumentSent($document)); } diff --git a/app/Notifications/Sale/Invoice.php b/app/Notifications/Sale/Invoice.php index 4c0033915..8c5ebc97b 100644 --- a/app/Notifications/Sale/Invoice.php +++ b/app/Notifications/Sale/Invoice.php @@ -35,10 +35,17 @@ class Invoice extends Notification */ public $attach_pdf; + /** + * List of document attachments to attach when sending the email. + * + * @var array + */ + public $attachments; + /** * Create a notification instance. */ - public function __construct(Document $invoice = null, string $template_alias = null, bool $attach_pdf = false, array $custom_mail = []) + public function __construct(Document $invoice = null, string $template_alias = null, bool $attach_pdf = false, array $custom_mail = [], $attachments = []) { parent::__construct(); @@ -46,6 +53,7 @@ class Invoice extends Notification $this->template = EmailTemplate::alias($template_alias)->first(); $this->attach_pdf = $attach_pdf; $this->custom_mail = $custom_mail; + $this->attachments = $attachments; } /** @@ -68,6 +76,17 @@ class Invoice extends Notification ]); } + // Attach selected attachments + if (! empty($this->invoice->attachment)) { + foreach ($this->invoice->attachment as $attachment) { + if (in_array($attachment->id, $this->attachments)) { + $message->attach($attachment->getAbsolutePath(), [ + 'mime' => $attachment->mime_type, + ]); + } + } + } + return $message; } diff --git a/resources/lang/en-GB/general.php b/resources/lang/en-GB/general.php index 373f92d87..21be355d0 100644 --- a/resources/lang/en-GB/general.php +++ b/resources/lang/en-GB/general.php @@ -230,6 +230,7 @@ return [ 'go_back' => 'Go back to :type', 'validation_error' => 'Validation error', 'dismiss' => 'Dismiss', + 'size' => 'Size', 'card' => [ 'cards' => 'Card|Cards', diff --git a/resources/views/modals/invoices/email.blade.php b/resources/views/modals/invoices/email.blade.php index 3691c9d8e..db03c2da4 100644 --- a/resources/views/modals/invoices/email.blade.php +++ b/resources/views/modals/invoices/email.blade.php @@ -1,15 +1,91 @@ - - - + + + + {{ trans('general.general') }} + - - - - - - - + @if ($invoice->attachment) + + {{ trans_choice('general.attachments', 2) }} + + @endif - + + + + + + + + + + + + + + + + + + + @if ($invoice->attachment) + + + + + + + + + + + + {{ trans('general.name') }} + + + + {{ trans('general.size') }} + + + + + + @foreach($invoice->attachment as $attachment) + + + + + + @if ($attachment->aggregate_type == 'image') +
+ {{ $attachment->basename }} +
+ @else +
+ attach_file +
+ @endif +
+ + + {{ $attachment->basename }} + + + + {{ $attachment->readableSize() }} + +
+ @endforeach +
+
+
+ @endif +
+