Laravel-excel: [QUESTION] - How to create and send an excel as an attachment?

Created on 28 Jan 2017  路  7Comments  路  Source: Maatwebsite/Laravel-Excel

Laravel 4.2

Hello, I would like to know if it has a way to send a newly created excel file as an email attachment? I can save it to disk, normally but how do I create this excel and attach it immediately to email?
I tried to use $ this-> export_planilha ($ id) -> download ('xls');

The program finish in this line.
But the code stops at this line. Thank you.

Most helpful comment

Here my snippet from Laravel 5.1, maybe it will help you.

        /** @var LaravelExcelWriter $file */

        $file = Excel::create("Waiting unvalidated Accounts -".Carbon::now()->getTimestamp(),
            function (LaravelExcelWriter $excel) use ($summary) {
                $excel->sheet('bookings', function (LaravelExcelWorksheet $sheet) use ($summary) {
                    $sheet->fromArray($summary);
                });
            });

        \Mail::send('mails.dummy',["data"=>"Report unvalidated Accounts"],function($m) use($file){
            $m->to('[email protected]')->subject('Report unvalidated Accounts');
            $m->attach($file->store("xls",false,true)['full']);
        });

All 7 comments

Here my snippet from Laravel 5.1, maybe it will help you.

        /** @var LaravelExcelWriter $file */

        $file = Excel::create("Waiting unvalidated Accounts -".Carbon::now()->getTimestamp(),
            function (LaravelExcelWriter $excel) use ($summary) {
                $excel->sheet('bookings', function (LaravelExcelWorksheet $sheet) use ($summary) {
                    $sheet->fromArray($summary);
                });
            });

        \Mail::send('mails.dummy',["data"=>"Report unvalidated Accounts"],function($m) use($file){
            $m->to('[email protected]')->subject('Report unvalidated Accounts');
            $m->attach($file->store("xls",false,true)['full']);
        });

Hay, I have used ->attach or ->attachData for attaching my excel report, but I got an error like this :
[2017-03-08 09:14:57] local.ERROR: ErrorException: fopen(C:\xampp\htdocs\webapp_4\storage\exports/Test 2017-03-08 09:14:54.xls): failed to open stream: Invalid argument in C:\xampp\htdocs\webapp_4\vendor\phpoffice\phpexcel\Classes\PHPExcel\Shared\OLE\PPS\Root.php:94
I also have tried ->store variation like these :
//->store('xlsx', storage_path('excel-folder'));
//->export('xls', storage_path('excel-folder'));
//->store('xls', false, true);
//->store('xls');
//->save(storage_path('excel-folder').'/test.xls');
//->save('xls', storage_path('excel-folder'));
//->save('xls')
But all of those ->store or ->save are failed :(
Is there another way ? Thanks

@biwerr idea works in 5.3 also:
mailable class build method:

 public function build()
    {

        return $this->view('emails.send-delivery-chart-address')
                     ->attach($this->excelData->store("xlsx",false,true)['full']);
    }

Where $excelData passed from controller:

$excelData = Excel::create('delivery_chart_for'.$tomorrow, function($excel) use($aAddressList) {

        $excel->sheet('delviery Address', function($sheet) use($aAddressList) {

            $sheet->fromArray($aAddressList, null, 'A1', false, false);
            $sheet->cells('A1:H1', function($cells) {

                $cells->setBackground('#2980b9');

            });

        });
        });

@biwerr thank you, your code works in laravel 5.3 as well.

Is there a v3 method for sending a spreadsheet as an attachment?

In v3 you can get the full path of the stored file using laravel's storage facade:

$filename = 'myfile.xlsx';
$disk = 'local';
Excel::store(new Export, $filename, $disk);
$fullPath = Storage::disk($disk)->path($filename);

Once you have the full path, you can attach the same way as the above examples.

To avoid writing to disk superfluously, use the string() method on LaravelExcelWriter which is what the Excel::create() method returns and pass it to $this->attachData() on the Mailable:

$csvContents = \Excel::create($csvFileName, function($excel) {
    $excel->setTitle('Title')->sheet(
        'Sheet 1',
        function($sheet) {
            $sheet->setOrientation('landscape');
            $sheet->appendRow(['header']);
            foreach($items as $item) {
                $sheet->appendRow(['data']);
            }
        }
    );
})->string('csv');

return $this->subject('Subject')
    ->from('[email protected]', 'Example')
    ->view('emails.example-email', [compact('data')])
    ->attachData($csvContents, $csvFileName, ['mime' => 'text/csv']);
Was this page helpful?
0 / 5 - 0 ratings

Related issues

daraghoshea picture daraghoshea  路  3Comments

amine8ghandi8amine picture amine8ghandi8amine  路  3Comments

ellej16 picture ellej16  路  3Comments

rossjcooper picture rossjcooper  路  3Comments

lucatamtam picture lucatamtam  路  3Comments