Attachment links

This commit is contained in:
David Bomba 2023-03-09 10:38:08 +11:00
parent 92294d8379
commit 686b48c42b
3 changed files with 36 additions and 15 deletions

View File

@ -14,12 +14,16 @@ namespace App\Services\Email;
use App\Models\Document; use App\Models\Document;
use Illuminate\Mail\Mailable; use Illuminate\Mail\Mailable;
use Illuminate\Mail\Attachment; use Illuminate\Mail\Attachment;
use Illuminate\Support\Facades\URL;
use Illuminate\Mail\Mailables\Content; use Illuminate\Mail\Mailables\Content;
use Illuminate\Mail\Mailables\Headers; use Illuminate\Mail\Mailables\Headers;
use Illuminate\Mail\Mailables\Envelope; use Illuminate\Mail\Mailables\Envelope;
class EmailMailable extends Mailable class EmailMailable extends Mailable
{ {
public int $max_attachment_size = 3000000;
/** /**
* Create a new message instance. * Create a new message instance.
* *
@ -53,6 +57,16 @@ class EmailMailable extends Mailable
*/ */
public function content() public function content()
{ {
$links = Document::whereIn('id',$this->email_object->documents)
->where('size', '>', $this->max_attachment_size)
->cursor()
->map(function ($document){
return "<a class='doc_links' href='" . URL::signedRoute('documents.public_download', ['document_hash' => $document->hash]) ."'>". $document->name ."</a>";
});
return new Content( return new Content(
view: $this->email_object->html_template, view: $this->email_object->html_template,
text: $this->email_object->text_template, text: $this->email_object->text_template,
@ -64,7 +78,8 @@ class EmailMailable extends Mailable
'logo' => $this->email_object->logo, 'logo' => $this->email_object->logo,
'signature' => $this->email_object->signature, 'signature' => $this->email_object->signature,
'company' => $this->email_object->company, 'company' => $this->email_object->company,
'greeting' => '' 'greeting' => '',
'links' => array_merge($this->email_object->links, $links->toArray()),
] ]
); );
} }
@ -78,18 +93,20 @@ class EmailMailable extends Mailable
{ {
$attachments = []; $attachments = [];
foreach ($this->email_object->attachments as $file) { $attachments = collect($this->email_object->attachments)->map(function ($file){
$attachments[] = Attachment::fromData(fn () => base64_decode($file['file']), $file['name']); return Attachment::fromData(fn () => base64_decode($file['file']), $file['name']);
} });
foreach($this->email_object->documents as $doc_id){ $documents = Document::whereIn('id',$this->email_object->documents)
$document = Document::find($doc_id); ->where('size', '<', $this->max_attachment_size)
->cursor()
->map(function ($document){
if($document) return Attachment::fromData(fn () => $document->getFile(), $document->name);
$attachments[] = Attachment::fromData(fn () => $document->getFile(), $document->name);
}
return $attachments; });
return $attachments->merge($documents)->toArray();
} }
/** /**

View File

@ -111,4 +111,6 @@ class EmailObject
public array $documents = []; public array $documents = [];
public ?string $template = null; //invoice //quote //reminder1 public ?string $template = null; //invoice //quote //reminder1
public array $links = [];
} }

View File

@ -167,13 +167,15 @@
</a> </a>
</div> </div>
@isset($links)
<div> <div>
@isset($links) <ul style="list-style-type: none;">
@foreach($links as $link) @foreach($links as $link)
{!! $link ?? '' !!}<br> <li>{!! $link ?? '' !!} <img height="15px" src="{{ asset('images/svg/dark/file.svg') }}"></li>
@endforeach @endforeach
@endisset </ul>
</div> </div>
@endisset
</div> </div>
</td> </td>
</tr> </tr>