php 如何将Excel文件附加到电子邮件中?

zqdjd7g9  于 2023-05-05  发布在  PHP
关注(0)|答案(3)|浏览(199)

我想我接近了,我只需要在邮件中传递$excelFile,但它一直说它未定义,然而当我传递$truckstop_post时,它通过了,但数据没有正确格式化,因为它还没有通过Excel::create。如何将\Excel::create的结果导入Mail::send??

public function truckstopPost()
{   
    $type = 'csv';


    $truckstop_post = Loadlist::select('pick_city', 'pick_state', 'delivery_city', 'delivery_state', 'trailer_type', 'pick_date', 'load_type', 'length', 'width', 'height', 'weight', 'offer_money', 'special_instructions', 'company_contact', 'contact_phone')->where('urgency', 'OPEN')->orderBy('id', 'desc')->get();

    $excelFile = \Excel::create('itransys', function($excel) use ($truckstop_post) {
        $excel->sheet('mySheet', function($sheet) use ($truckstop_post)
        {
            $sheet->fromArray($truckstop_post);

        });

       $info = Load::find(8500);

       $info = ['info'=>$info];

       Mail::send(['html'=>'email.invoice_email_body'], $info, function($message) use ($info, $excelFile){

        $message->to('mike@gmail.com')->subject('subject');

        $message->from('mike@gmail.com', \Auth::user()->name);

        $message->attachData($excelFile, 'Invoice.csv');

        });

        });

    return back()->with('status', 'You Posted Truckstop!');

}

这就是我将$truckstop_post传递给attachData()的结果,当然这不是一个格式很好的csv文件

lpwwtiir

lpwwtiir1#

你可以这样做

use Maatwebsite\Excel\Excel as BaseExcel;
use Maatwebsite\Excel\Facades\Excel;

...

$filename = "my_file.csv";

$attachment = Excel::raw(
    new PurchaseOrderLinesExport($this->data), 
    BaseExcel::CSV
);
$subject = "Purchase Order"

return $this->from($this->employee->email)
            ->subject("Purchase Order)
            ->view('emails.view')
            ->attachData($attachment, $filename);

Excel::raw()方法创建/写入临时文件,然后获取其内容并在删除该文件后删除。

如果你使用的是laravel,第二个解决方案会是这样的:

public function build()
{
    return $this->markdown('emails.report')
        ->attach(
            Excel::download(
                new AuditReport($this->audit), 
                'report.xlsx'
            )->getFile(), ['as' => 'report.xlsx']
        );
}

Excel::download()返回一个BinaryFileResponse,这就是为什么它不能直接工作,但你可以抓取文件。

b4qexyjb

b4qexyjb2#

public function post()
{   
    $type = 'csv';

    $create_excel = List::select('pick_city', 'pick_state', 'delivery_city', 'delivery_state')->where('urgency', 'OPEN')->orderBy('id', 'desc')->get()->toArray();

    $excelFile = \Excel::create('itrans', function($excel) use ($create_excel) {
        $excel->sheet('mySheet', function($sheet) use ($create_excel)
        {
            $sheet->fromArray($create_excel);

        });

    })->download($type);

    Mail::send(['html'=>'email.email_body'] function($message) {

     $message->to('example@gmail.com')

    ->subject('Email Subject Line');

    $message->from('daniel@twbb.com', 'Daniel');

    $message->attachData($excelFile, 'Invoice.csv');

});

    return $excelFile;

}
c2e8gylq

c2e8gylq3#

从Laravel 9开始,可以在Mailable类中使用attachments()方法:

use Illuminate\Mail\Mailables\Attachment;
use Maatwebsite\Excel\Excel as BaseExcel;
use Maatwebsite\Excel\Facades\Excel;

public function attachments(): array
{
    return [
        Attachment::fromData(fn () => Excel::raw(new UsersExport(), BaseExcel::XLSX), 'report.xlsx'),
    ];
}

相关问题