Skip to content

Commit

Permalink
allow attaching uploaded files when sending mails
Browse files Browse the repository at this point in the history
  • Loading branch information
Hendrik Hagendorn committed May 27, 2023
1 parent ece0c27 commit 5e1ecb8
Show file tree
Hide file tree
Showing 5 changed files with 115 additions and 13 deletions.
7 changes: 4 additions & 3 deletions app/Http/Requests/Common/CustomMail.php
Original file line number Diff line number Diff line change
Expand Up @@ -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',
];
}
}
7 changes: 6 additions & 1 deletion app/Jobs/Document/SendDocumentAsCustomMail.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,15 @@ public function handle(): void
$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));
}
Expand Down
21 changes: 20 additions & 1 deletion app/Notifications/Sale/Invoice.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,17 +35,25 @@ 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();

$this->invoice = $invoice;
$this->template = EmailTemplate::alias($template_alias)->first();
$this->attach_pdf = $attach_pdf;
$this->custom_mail = $custom_mail;
$this->attachments = $attachments;
}

/**
Expand All @@ -68,6 +76,17 @@ public function toMail($notifiable): MailMessage
]);
}

// 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;
}

Expand Down
1 change: 1 addition & 0 deletions resources/lang/en-GB/general.php
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,7 @@
'go_back' => 'Go back to :type',
'validation_error' => 'Validation error',
'dismiss' => 'Dismiss',
'size' => 'Size',

'card' => [
'cards' => 'Card|Cards',
Expand Down
92 changes: 84 additions & 8 deletions resources/views/modals/invoices/email.blade.php
Original file line number Diff line number Diff line change
@@ -1,15 +1,91 @@
<x-form id="form-email" :route="[$store_route, $invoice->id]">
<x-form.section>
<x-slot name="body">
<x-form.group.text name="to" label="{{ trans('general.to') }}" value="{{ $invoice->contact->email }}" form-group-class="sm:col-span-6" />
<x-tabs active="general" class="grid grid-cols-{{ $invoice->attachment ? '2' : '1' }}" override="class" ignore-hash>
<x-slot name="navs">
<x-tabs.nav id="general">
{{ trans('general.general') }}
</x-tabs.nav>

<x-form.group.text name="subject" label="{{ trans('settings.email.templates.subject') }}" value="{{ $notification->getSubject() }}" form-group-class="sm:col-span-6" />
@if ($invoice->attachment)
<x-tabs.nav id="attachments">
{{ trans_choice('general.attachments', 2) }}
</x-tabs.nav>
@endif
</x-slot>

<x-slot name="content">
<x-tabs.tab id="general">
<x-form.section>
<x-slot name="body">
<x-form.group.text name="to" label="{{ trans('general.to') }}" value="{{ $invoice->contact->email }}" form-group-class="sm:col-span-6" />

<x-form.group.text name="subject" label="{{ trans('settings.email.templates.subject') }}" value="{{ $notification->getSubject() }}" form-group-class="sm:col-span-6" />

<x-form.group.editor name="body" label="{{ trans('settings.email.templates.body') }}" :value="$notification->getBody()" rows="5" data-toggle="quill" form-group-class="sm:col-span-6 mb-0" />

<x-form.group.checkbox name="user_email" :options="['1' => trans('general.email_send_me', ['email' => user()->email])]" checkbox-class="col-span-6" />

<x-form.input.hidden name="document_id" :value="$invoice->id" />
</x-slot>
</x-form.section>
</x-tabs.tab>

@if ($invoice->attachment)
<x-tabs.tab id="attachments">
<x-table>
<x-table.thead>
<x-table.tr class="flex items-center px-1">
<x-table.th class="ltr:pr-6 rtl:pl-6 hidden sm:table-cell w-1/12" override="class">
</x-table.th>

<x-table.th class="w-1/6">
</x-table.th>

<x-table.th class="w-4/6">
{{ trans('general.name') }}
</x-table.th>

<x-table.th class="w-1/6">
{{ trans('general.size') }}
</x-table.th>
</x-table.tr>
</x-table.thead>

<x-form.group.editor name="body" label="{{ trans('settings.email.templates.body') }}" :value="$notification->getBody()" rows="5" data-toggle="quill" form-group-class="sm:col-span-6 mb-0" />
<x-table.tbody>
@foreach($invoice->attachment as $attachment)
<x-table.tr id="method-{{ $attachment->id }}">
<x-table.td class="ltr:pr-6 rtl:pl-6 hidden sm:table-cell w-1/12" override="class">
<input type="checkbox"
id="attachment-{{ $attachment->id }}"
name="{{ $attachment->id }}"
class="rounded-sm text-purple border-gray-300 cursor-pointer disabled:bg-gray-200 focus:outline-none focus:ring-transparent"
data-field="attachments"
@input="e => form.attachments[e.target.name] = e.target.checked | 0">
</x-table.td>
<x-table.td class="w-1/6">
@if ($attachment->aggregate_type == 'image')
<div class="avatar-attachment">
<img src="{{ route('uploads.get', $attachment->id) }}" alt="{{ $attachment->basename }}" class="avatar-img h-full rounded object-cover">
</div>
@else
<div class="avatar-attachment">
<span class="material-icons text-base">attach_file</span>
</div>
@endif
</x-table.td>

<x-form.group.checkbox name="user_email" :options="['1' => trans('general.email_send_me', ['email' => user()->email])]" checkbox-class="col-span-6" />
<x-table.td class="w-4/6">
{{ $attachment->basename }}
</x-table.td>

<x-form.input.hidden name="document_id" :value="$invoice->id" />
<x-table.td class="w-1/6">
{{ $attachment->readableSize() }}
</x-table.td>
</x-table.tr>
@endforeach
</x-table.tbody>
</x-table>
</x-tabs.tab>
@endif
</x-slot>
</x-form.section>
</x-tabs>
</x-form>

0 comments on commit 5e1ecb8

Please sign in to comment.