invoices = $invoices; $this->company = $company; $this->user = $user; $this->settings = $company->settings; } /** * Execute the job. * * @return void * @throws \ZipStream\Exception\FileNotFoundException * @throws \ZipStream\Exception\FileNotReadableException * @throws \ZipStream\Exception\OverflowException */ public function handle() { # create new zip object $zip = new ZipArchive(); $invitation = $this->invoices->first()->invitations->first(); $path = $this->invoices->first()->client->invoice_filepath($invitation); $file_name = date('Y-m-d').'_'.str_replace(' ', '_', trans('texts.invoices')).'.zip'; $tmp_file = @tempnam('.', ''); $zip->open($tmp_file , ZipArchive::OVERWRITE); # loop through each file foreach ($this->invoices as $invoice) { $inv = $invoice->invitations->first(); # download file $download_file = file_get_contents($invoice->pdf_file_path($inv, 'url', true)); #add it to the zip $zip->addFromString(basename($invoice->pdf_file_path($inv)), $download_file); } # close zip $zip->close(); Storage::put($path.$file_name, file_get_contents($tmp_file)); $nmo = new NinjaMailerObject; $nmo->mailable = new DownloadInvoices(Storage::url($path.$file_name), $this->company); $nmo->to_user = $this->user; $nmo->settings = $this->settings; $nmo->company = $this->company; NinjaMailerJob::dispatch($nmo); UnlinkFile::dispatch(config('filesystems.default'), $path.$file_name)->delay(now()->addHours(1)); } }