Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 1 addition & 5 deletions src/Core/Listeners/BeforeSendingMailListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,6 @@ private function storeAttachments(array $attachments, BetterEmail $mail): void
}

$disk = config('filament-better-mails.mails.logging.attachments.disk', 'local');
$root = config('filament-better-mails.mails.logging.attachments.root', 'mails/attachments');

foreach ($attachments as $part) {
$filename = $part->getFilename() ?? 'attachment';
Expand All @@ -64,10 +63,7 @@ private function storeAttachments(array $attachments, BetterEmail $mail): void
'size' => strlen($content),
]);

Storage::disk($disk)->put(
$root.'/'.$attachment->getKey().'/'.$filename,
$content,
);
Storage::disk($attachment->disk)->put($attachment->storage_path, $content);
}
}
}
4 changes: 3 additions & 1 deletion src/Core/Models/BetterEmailAttachment.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,9 @@ public function mail(): BelongsTo

public function getStoragePathAttribute(): string
{
return rtrim(config('filament-better-mails.logging.attachments.root'), '/').'/'.$this->getKey().'/'.$this->filename;
$root = config('filament-better-mails.mails.logging.attachments.root', 'mails/attachments');

return rtrim($root, '/').'/'.$this->getKey().'/'.$this->filename;
}

public function getFileDataAttribute(): string
Expand Down
74 changes: 74 additions & 0 deletions tests/Feature/Core/Models/BetterEmailAttachmentTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
<?php

use Basement\BetterMails\Core\Listeners\BeforeSendingMailListener;
use Basement\BetterMails\Core\Models\BetterEmailAttachment;
use Basement\BetterMails\Tests\Fixtures\Mail\FakeMail;
use Illuminate\Mail\Events\MessageSending;
use Illuminate\Support\Facades\Storage;
use Symfony\Component\Mime\Email;

function sendMailWithAttachment(string $filename = 'report.docx', string $content = 'file contents'): BetterEmailAttachment
{
$email = (new Email)
->subject('Test Subject')
->from('from@example.com')
->to('richard@3points.com')
->html('<h1>Test HTML</h1>')
->attach($content, $filename, 'application/vnd.openxmlformats-officedocument.wordprocessingml.document');

(new BeforeSendingMailListener)->handle(new MessageSending($email, [
'mailer' => 'log',
'__laravel_mailable' => FakeMail::class,
]));

return BetterEmailAttachment::query()->latest('id')->firstOrFail();
}

it('reads an attachment back from the path it was written to', function () {
Storage::fake('local');

$attachment = sendMailWithAttachment();

Storage::disk('local')->assertExists($attachment->storage_path);
expect($attachment->file_data)->toBe('file contents');
});

it('prefixes the storage path with the configured attachment root', function () {
Storage::fake('local');

$attachment = sendMailWithAttachment();

expect($attachment->storage_path)
->toBe('mails/attachments/'.$attachment->getKey().'/report.docx');
});

it('honours a custom attachment root on both write and read', function () {
Storage::fake('local');
config()->set('filament-better-mails.mails.logging.attachments.root', 'custom/root/');

$attachment = sendMailWithAttachment();

expect($attachment->storage_path)->toBe('custom/root/'.$attachment->getKey().'/report.docx');
Storage::disk('local')->assertExists($attachment->storage_path);
});

it('falls back to the default root when the config key is absent', function () {
Storage::fake('local');

$attachment = sendMailWithAttachment();

config()->set('filament-better-mails.mails.logging.attachments', null);

expect($attachment->fresh()->storage_path)
->toBe('mails/attachments/'.$attachment->getKey().'/report.docx');
});

it('can stream the attachment as a download', function () {
Storage::fake('local');

$attachment = sendMailWithAttachment();

$response = $attachment->downloadFileFromStorage();

expect($response->getStatusCode())->toBe(200);
});