Attach zip to email (pwd protection available in ˆ7.2)
Wed Dec 02 2020 02:42:42 GMT+0000 (UTC)
Saved by @eneki
// Route code Route::get('/test/excel', function() { $zip_file = 'invoices.zip'; // Name of our archive to download // Initializing PHP class $zip = new \ZipArchive(); $zip->open($zip_file, \ZipArchive::CREATE | \ZipArchive::OVERWRITE); $invoice_file = 'app/exports/file.xls'; // This is a storage file path - as in, storage/app/exports/file.xls // Adding file: second parameter is what will the path inside of the archive // So it will create another folder called "storage/" inside ZIP, and put the file there. $zip->addFile(storage_path($invoice_file), $invoice_file); $zip->close(); // We return the file immediately after download event(new TestExcel($zip_file)); flash()->success('test'); return redirect('/contacts'); }); // Event code <?php namespace App\Events; use App\Events\Event; use Illuminate\Queue\SerializesModels; use Illuminate\Contracts\Broadcasting\ShouldBroadcast; class TestExcel extends Event { use SerializesModels; /* * get the require info for sending email */ public $contact; public $errors; /** * Create a new event instance. * * @return void */ public function __construct($zip_file) { $this->zip_file = $zip_file; } /** * Get the channels the event should be broadcast on. * * @return array */ public function broadcastOn() { return []; } } // Listener code <?php namespace App\Listeners; use App\Events\Interactions\InteractionsEmailError; use App\Events\TestExcel; use App\Mail\Mailers\ComplexMailable; use App\Mail\Mailers\SimpleMailable; use Illuminate\Support\Facades\Mail; class WhenTestExcel { /** * Create the event listener. * * @return void */ public function __construct() { // } /** * Handle the event. * * @param InteractionsEmailError $event * @return void */ public function handle(TestExcel $event) { $this->sendInteractionsEmail($event->zip_file); } public function sendInteractionsEmail($zip_file) { $data = [ 'view' => 'emails.leads.referrals.referrers_to_be_contacted', 'subject' => 'CRM Lead Referrers Report', 'attachments' => $zip_file, 'content' => 'Find attached details of the referrers who requested to be contacted.', 'to' => 'clientservice@cytonn.com', ]; Mail::send(new ComplexMailable($data)); } }
Comments