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
toeknee8mo 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
TheNastyPastyOP8mo 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