Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

hjortur17's avatar

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.

0 likes
4 replies
hjortur17's avatar

Yes, not much about the attachments. @tray2 - Do you think I need to change how I send the message like they do it in the docs?

hjortur17's avatar

I modified my Mail sender, now I'm storing each file to the local disk and then getting it and attaching it. But I am not receiving the emails but no error in the console, any ideas?

Here is the code:

public function update(Request $request)
	{
		$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::loadHtml('<h1>Hello world!</h1>');
	
		$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]")
				->attach($pdf_file->stream())
				->send(new BookingConfirmation($booking));
		} else {
			Mail::send(new BookingConfirmation($booking), $booking, function ($message) use ($booking, $file) {
				$message->from('[email protected]', 'Bílahótel');
				$message->to($booking['email'])->cc('[email protected]');
				$message->subject("Bókun {$booking['id']} staðfest | Bílahótel");
				$message->attach($file);
			});
		}

		return $file;
	}
hjortur17's avatar
hjortur17
OP
Best Answer
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.