Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[QUESTION] Attach generated excel with V 3.1 #1910

Closed
link08 opened this issue Nov 22, 2018 · 8 comments
Closed

[QUESTION] Attach generated excel with V 3.1 #1910

link08 opened this issue Nov 22, 2018 · 8 comments

Comments

@link08
Copy link

link08 commented Nov 22, 2018

Versions

  • PHP version: 7.1.7
  • Laravel version: 5.7.13
  • Package version: 3.1

Description

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('to@example.com')->send(new SendMail($attachment));

# Inside SendMail.php
public function __construct($attachment)
{
    $this->attachment = $attachment;
}

public function build()
{

    $mail = $this->from('from@example.com')
        ->subject('test attachment')
        ->markdown('emails.test_mail')
        ->attach($this->attachment);

    return $mail;
}
@Kyosfonica
Copy link

I have the exact same question.

@link08
Copy link
Author

link08 commented Nov 22, 2018

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('from@example.com')
        ->subject('test attachment')
        ->markdown('emails.test_mail')
        ->attach($this->attachment, , ['as' => $filename]);

@rogercbe
Copy link

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 😃

@marcelopm
Copy link

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

Watch out for the server's tmp dir though as the file gets written to disk.

Export should be a public method.

@FrankPeters
Copy link
Member

Thanks guys for helping each other out! 👍

@PZ01
Copy link

PZ01 commented Jun 5, 2020

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);

@KinyuaMwaniki
Copy link

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 smiley

Thanks for this, it worked for me.

@tusfendi
Copy link

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

8 participants