Have you checked the docs?
Jul 1, 2021
4
Level 14
Help using Mail
Hi, I'm trying to attach a PDF file to each mail sent out. I'm trying to use DOMPDF Wrapper for Laravel for generating the PDF file and then attaching it when I'm sending it out but I'm receiving "Undefined variable $booking in booking_confirmed.blade.php"
What could it be, I think i'm sending it in correctly. Here is my code:
$booking = Booking::where('randomKey', '=', $request->reference)->first();
if ($request->authcode && $request->cardbrand && $request->card4) {
$booking->paid = true;
$booking->korta_authcode = $request->authcode;
$booking->cardbrand = $request->cardbrand;
$booking->card4 = $request->card4;
$booking->save();
}
$pdf_file = PDF::loadView('emails.booking_confirmed', $booking);
Mail::to($booking->email)
->cc("[email protected]")
->bcc("[email protected]")
->attach($pdf_file, ['as' => 'Afrit af bókun'])
->send(new BookingConfirmation($booking));
BTW, the query is finding the booking.
Level 14
I needed to add one line to the mail controller like this :
->attachFromStorage("Bókun {$this->booking->id}.pdf");
Then my controller sending the email ended something like this:
$pdf_file = PDF::loadView('pdf.booking_confirmed', compact('booking'));
$fileName = "Bókun {$booking['id']}.pdf";
Storage::disk('local')->put($fileName, $pdf_file->download($fileName));
$file = Storage::get($fileName);
if (app()->environment('production')) {
Mail::to($booking->email)
->cc("[email protected]")
->bcc("[email protected]")
->send(new BookingConfirmation($booking));
} else {
Mail::to($booking->email)->send(new BookingConfirmation($booking));
}
Please or to participate in this conversation.