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

7

我认为我已经接近成功了,我只需要将$excelFile传递给Mail,但它一直显示未定义,但是当我传递$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文件。

enter image description here


到底出了什么问题?是生成Excel表格吗?还是将其与电子邮件附加发送? - Dekel
如何将其附加到电子邮件中,$message->attachData(???, 'Invoice.csv'); 中应该放什么?然后 ->download($type); 会发生什么? - mcornille
请删除与问题文件附件无关的所有内容。假设您有文件X并希望将其附加。 - Dekel
@Paras 所以问题应该是“我如何将路由中的数据存储为文件?”这就是为什么我问了具体是什么问题。 - Dekel
请发布您的错误信息和堆栈跟踪。 - Paras
显示剩余2条评论
3个回答

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

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, 因此不能直接使用,但你可以获取该文件。


2
谢谢,Excel::raw(new ExcelExport($this->data), \Maatwebsite\Excel\Excel::XLSX); 结合 attachData 对我很有帮助。 - Tenarius

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;

}

请发布dd($create_excel)的输出结果。 - Paras
在初始化$create_excel变量并在调用\Excel::create之前,你能否插入dd($create_excel)语句? - Paras

0

从 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'),
    ];
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接