Hello,
how is it possible attach an excel file generated with the version 3.1?
In the previous version (v.2) it was possible through create method without "->download()"
I'm trying in this way, but it doesn't work:
$filename = 'text.xlsx';
$attachment = Excel::download(new TestExport(), $filename);
\Mail::to('[email protected]')->send(new SendMail($attachment));
# Inside SendMail.php
public function __construct($attachment)
{
$this->attachment = $attachment;
}
public function build()
{
$mail = $this->from('[email protected]')
->subject('test attachment')
->markdown('emails.test_mail')
->attach($this->attachment);
return $mail;
}
I have the exact same question.
I've found a solution changing from protected to public the export() method inside the file:
maatwebsite\excel\src\Excel.php
on row: 166
in this way, the code can be:
````
$attachment = Excel::export(new TestExport(), $filename);
[...]
$mail = $this->from('[email protected]')
->subject('test attachment')
->markdown('emails.test_mail')
->attach($this->attachment, , ['as' => $filename]);
````
I've found a solution to make it work without having to modify the Excel class and its protected method. You can call getFile()
from the Excel::download(/**/)
. The code looks like so:
public function build()
{
return $this->markdown('emails.report')
->attach(
Excel::download(
new AuditReport($this->audit),
'report.xlsx'
)->getFile(), ['as' => 'report.xlsx']
);
}
Excel::download()
returns a BinaryFileResponse
that's why it doesn't work directly, but you can grab the file.
Hope it helps 😃
I've found a solution to make it work without having to modify the Excel class and its protected method. You can call
getFile()
from theExcel::download(/**/)
. The code looks like so:public function build() { return $this->markdown('emails.report') ->attach( Excel::download( new AuditReport($this->audit), 'report.xlsx' )->getFile(), ['as' => 'report.xlsx'] ); }
Excel::download()
returns aBinaryFileResponse
that's why it doesn't work directly, but you can grab the file.Hope it helps
Watch out for the server's tmp dir though as the file gets written to disk.
Export should be a public method.
Thanks guys for helping each other out! 👍
Here is how I do it without downloading/creating the file:
use Maatwebsite\Excel\Excel as BaseExcel;
use Maatwebsite\Excel\Facades\Excel;
...
$filename = "{$this->po->po_memo}.xlsx";
$attachment = Excel::raw(new PurchaseOrderLinesExport($this->po), BaseExcel::XLSX);
$subject = "Purchase Order Invoice"
return $this->from($this->po->employee->email)
->subject($subject)
->view('emails.pom.send')
->text('emails.pom.send')
->attachData($attachment, $filename);
I've found a solution to make it work without having to modify the Excel class and its protected method. You can call
getFile()
from theExcel::download(/**/)
. The code looks like so:public function build() { return $this->markdown('emails.report') ->attach( Excel::download( new AuditReport($this->audit), 'report.xlsx' )->getFile(), ['as' => 'report.xlsx'] ); }
Excel::download()
returns aBinaryFileResponse
that's why it doesn't work directly, but you can grab the file.Hope it helps smiley
Thanks for this, it worked for me.
use Maatwebsite\Excel\Excel as BaseExcel; use Maatwebsite\Excel\Facades\Excel; ... $filename = "{$this->po->po_memo}.xlsx"; $attachment = Excel::raw(new PurchaseOrderLinesExport($this->po), BaseExcel::XLSX);
Thanks a lot. it's work for me
Most helpful comment
I've found a solution to make it work without having to modify the Excel class and its protected method. You can call
getFile()
from theExcel::download(/**/)
. The code looks like so:Excel::download()
returns aBinaryFileResponse
that's why it doesn't work directly, but you can grab the file.Hope it helps 😃