How to send excel file as an attachment in email in laravel?

Viewed 767

This is my excel file in public folder. Heres the directory structure

enter image description here

The file name is user_products.xlsx. I need to send it as an attachment. Here is the mail class's build function

public function build()
{
    // dd($this->data);
    return $this->subject('Subject:User Cart Inquiry')->view('emails.sendUserSubmitMail')
    ->attach(public_path('user_products.xlsx'), [
        'as' => 'user products.xlsx',
        'mime' => 'application/xlsx',
    ])
    ->with('data', $this->data);
}

The mail is working fine, but when i added the ->attach(), its showing error. The error is

message: "Unable to open file for reading [/opt/lampp/htdocs/RC/public/user_products.xlsx]"

Please help. Thank You!

1 Answers

Never give 777 permission on production. Try this.

# sudo adduser $USER www-data
# sudo chown $USER:www-data -R /opt/lampp/htdocs
# sudo chmod 755 -R /opt/lampp/htdocs

And now change the details in apache.

# sudo nano /opt/lampp/etc/httpd.conf

to this

User username
Group www-data

Change username with your currently logged in user, to see that execute this:

# whoami
Related