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

时间:2017-01-20 16:42:41

标签: php laravel email-attachments maatwebsite-excel

我认为我很接近,我只需要在Mail中传递$ excelFile但它一直说它未定义,但是当我传递$ truckstop_post时它会通过但是数据格式不正确bc它没有通过Excel ::创造了。如何将\ Excel :: create的结果导入Mail :: send ??

<script language='JavaScript' src='http://scripts.hashemian.com/js/countdown.js'></script>'";

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

enter image description here

1 个答案:

答案 0 :(得分: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;

}