happiness's avatar

Attach file to mail notification and display it in view version too

What I am trying to do: I have a procedure in my workflow the user goes through to create, with various steps of inserting data. In each step the user is required to upload a file. Let's say that when step 3 is completed, I want to create a notification that is send by mail with a message and the relative file attached. I also want to have a view in my app where a list of all the notifications the user has received is displayed, and when each of them clicked, to display the notification message (the one that was send by mail) and a link to the relative file. I.e. I want the user to be able to access his notifications both by email and through the app.

What troubles me: The attach() method for the MailMessage, needs an absolute path to the file, which is in the following format:

my-local-disk-path/storage/app/files/procedure/[procedure_id]/documents/[step_id]/[file_id]/[name_of_file]

I "construct" the string

 $path =  '\\files\\procedure\\'.$procedure_id.'\\documents\\'.$step_id.'\\'.$file_id;

in the notification __construct function and then do

$path = Storage::path($path.'\\'.$name_of_file);

and add

->attach($path)

to the MailMessage. I also store the notification to database with toArray().

I am now trying to build a view for the notification, which includes a link to the relative file. I have tried various things, but before presenting them (since they failed), I would like to ask your opinions on how to implement this.

0 likes
1 reply
alden8's avatar

In general, the following logic can be seen:

-- store file Information

-- generate download links in the view

<a href="{{ route('download-file', ['file_path' => $notification->file_path]) }}">
     {{ $notification->file_name }}
</a>

-- implement file download handling (route with Storage::download($file_path)

-- attach files to emails

$absolute_path = Storage::path($notification->file_path);
$message->attach($absolute_path);

-- on the local it is better to use Storage::disk('local')->path() for absolute paths, and on production ensure proper configuration of the storage path for the production environment

1 like

Please or to participate in this conversation.