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

mco*_*lle 7 php email-attachments laravel maatwebsite-excel

我想我很接近,我只需要在 Mail 中传递 $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!');

}
Run Code Online (Sandbox Code Playgroud)

如果我将 $truckstop_post 传递到 AttachData() 中,结果就是这样,但当然这不是一个格式良好的 csv 文件

在此输入图像描述

aju*_*o91 10

你可以这样做

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);
Run Code Online (Sandbox Code Playgroud)

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

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

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

Excel::download()返回 aBinaryFileResponse这就是它不能直接工作的原因,但您可以抓取该文件。

  • 谢谢,`Excel::raw(new ExcelExport($this->data), \Maatwebsite\Excel\Excel::XLSX);` 与 `attachData` 结合起来为我工作。 (2认同)

Par*_*ras 1

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;

}
Run Code Online (Sandbox Code Playgroud)