Attach file with original name to mail throws exception

Hello, I am trying for hours now to attach files to a mail, but without success. I am using the filament 3
->storeFileNamesIn('attachment_names')
->storeFileNamesIn('attachment_names')
function. Code which works
$attachments = [];

foreach ($this->template->attachments as $attachment) {
$attachments[] = Attachment::fromStorageDisk('local', $attachment);
}

return $attachments;
$attachments = [];

foreach ($this->template->attachments as $attachment) {
$attachments[] = Attachment::fromStorageDisk('local', $attachment);
}

return $attachments;
But I want to add the stored file names and that does not work. I get the following exception
Symfony\Component\Mime\Email::attachFromPath(): Argument #1 ($path) must be of type string, App\Mail\UserTemplate given
Symfony\Component\Mime\Email::attachFromPath(): Argument #1 ($path) must be of type string, App\Mail\UserTemplate given
public function attachments(): array
{
$attachments = [];
$attachment_names = $this->template->attachment_names;

foreach ($this->template->attachments as $attachment) {
$value = $attachment_names[$attachment] ?? null;
$attachments[] = $this->attachFromStorageDisk('local', $attachment, $value);
}

return $attachments;
}
public function attachments(): array
{
$attachments = [];
$attachment_names = $this->template->attachment_names;

foreach ($this->template->attachments as $attachment) {
$value = $attachment_names[$attachment] ?? null;
$attachments[] = $this->attachFromStorageDisk('local', $attachment, $value);
}

return $attachments;
}
Please someone can help Maybe there is a better way to attach files in filament 3 I dont know of.
2 Replies
toeknee
toeknee3mo ago
this isn't a filament issue You need to provide the filepath for the document to send it, you are returning the UserTemplate model. $attachment needs to be the filepath /var/www/mysite/storage/files/myfile.pdf
TheNastyPasty
TheNastyPasty3mo ago
Got it thanks Did it like so
public function attachments(): array
{
$attachments = [];
$attachment_names = $this->template->attachment_names;

foreach ($this->template->attachments as $attachment) {
$value = $attachment_names[$attachment] ?? null;
$attachments[] = Attachment::fromStorageDisk('local', $attachment)->as($value);
}

return $attachments;
}
public function attachments(): array
{
$attachments = [];
$attachment_names = $this->template->attachment_names;

foreach ($this->template->attachments as $attachment) {
$value = $attachment_names[$attachment] ?? null;
$attachments[] = Attachment::fromStorageDisk('local', $attachment)->as($value);
}

return $attachments;
}
Want results from more Discord servers?
Add your server
More Posts
Styling forms modal forms created with createOptionsForm()I have a modal for creating contacts, if a company does not exist, I want to be able to create on onHow to create a website and have the filamentphp as SASS product?Hi guys, I'd like to create a website with **statamic cms** (laravel) and also give access to filameUnable to set checkbox default value to trueI have this but it's unchecked: ```php Forms\Components\Checkbox::make('is_admin') ->inline(trueSection with reactive fields forces collapse after field value changes.I have a use case where I would want the heading of Section to reflect values of fields under it. ThTable with a modal action that should contain a custom table-like form, is that even possible?I have a table showing users, with an Action that opens a modal. I want the modal to contain a list Admin PanelIf the Admin is logged in currently in a session and the super admin Deactivates the admin's accountIs it possible for filament that I will show all the data in different panel? my foreign key is id.The Accruals table will also be shown in Draft Bill table , the foreign id is the reference number.How can i change this default text into popup Relationship?Hi! someone can help me? I did'nt find this into the documentation how to change this text (yelow) wHow can i remove this default label text in resourceHow can i remove this default label text in resourceRepeater Duplicate queries when adding new itemMy question is the following, why? when you add a new item to the repeater it executes the ->visible