how to send excel file with email in laravel when we run the command

91 Views Asked by At

So I want to send a email in a particular time, when my command runs and with that email, i want to send an excel file with that email,i also have some data which i want to append in the excel file, i have used Excel::download() method but it creates the excel file in this path "storage/framework/laravel-excel",with the name like "laravel-excel-9ssyjrYnrNNOBU2wfi5eaGtXsRySkj4X" which i don't want, i want to store that excel file somewhere else in the storage folder and then retrieve it and send it with the email.

As of Now I have deleted all the files from the path "storage/framework/laravel-excel/" first and then created the new excel file which will store in the path which is mentioned in previous line , and then fetch the new file with the email and send it. But this approach is not looking good or code reviewer will definitely dissappointed by this code. Is there any better solution to this , i already tried store method but it doesn't give me result

Public function build(){

        $directoryPath = storage_path('framework/laravel-excel/');

        $files = File::glob("{$directoryPath}*.xlsx");
        if($files){
            File::delete($files);
        }
       
        Excel::download(new SellerPayoutExport([$this->record]) , 'SellerPayout'. '.'. 'xlsx');
        $latestFile = reset($files);
    
        return $this->from(core()->getSenderEmailDetails()['email'], core()>getSenderEmailDetails()  ['name'])
            ->to('[email protected]')
            ->subject(trans('customshop::app.mail.seller-payout.subject'))
            ->view('customshop::emails.sales.seller-payouts')
            ->attach($latestFile, [
                'as' => 'seller_payouts.xlsx',
                'mime' => 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
            ]
        );  
}
1

There are 1 best solutions below

3
Shima.dev On

When you want to customize the storage location of the generate file, You can use the store method provided by Laravel Excel to save the file to a specific location Instead of deleting files from the default path.

public function build()
{
    // Define your custom path here
    $customPath = storage_path('app/excel-files/');

    // Make sure the directory exists
    if (!File::exists($customPath)) {
        File::makeDirectory($customPath, 0755, true, true);
    }

    // Generate and store the Excel file
    $excelFileName = 'SellerPayout.xlsx';
    $excelFilePath = $customPath . $excelFileName;
    
    Excel::store(new SellerPayoutExport([$this->record]), $excelFileName, 'local');

    return $this->from(core()->getSenderEmailDetails()['email'], core()->getSenderEmailDetails()['name'])
        ->to('[email protected]')
        ->subject(trans('customshop::app.mail.seller-payout.subject'))
        ->view('customshop::emails.sales.seller-payouts')
        ->attach($excelFilePath, [
            'as'   => $excelFileName,
            'mime' => 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
        ]);
}

This way, you won't have to delete files from the default path, and the generated Excel file will be stored in your desired location. Adjust the $customPath as needed for your application's structure.

I'm not familiar with the specifics of your application, but if it happens to interact with various storage systems, you can use Storage::disk() in your code. In the above example, I've configured the default storage disk to be 'local'.

cheers