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

deladels's avatar

Issue using Mailable and markdowns

i am trying to send an email when a users books a schedule. (i want to use the simple way so this is triggered when a button is clicked)

BookingController.php

    public function email(Request $request, $bookingId){

        $booking = Booking::find($bookingId);

        Mail::to($request->email)->send(new ScheduleBooked($booking));

        return redirect('uschedules');
    }

ScheduleBooked.php (Mailable)

<?php

namespace App\Mail;

use Illuminate\Bus\Queueable;
use Illuminate\Mail\Mailable;
use Illuminate\Queue\SerializesModels;
use Illuminate\Contracts\Queue\ShouldQueue;
use App\Booking;

class ScheduleBooked extends Mailable
{
    use Queueable, SerializesModels;


    public $booking;



    public function __construct(Booking $booking)
    {
        $this->booking = $booking;
    }

    public function build()
    {
        return $this->markdown('emails.schedules.booked')->with([

                        'lastname'  => $this->booking->lastName,
                        'firstname' => $this->booking->firstName,
        ]);
    }
}

route file.

Route::get('/email', 'BookingsController@email')->name('sendEmail');

i don't get any errors but the emails aren't being sent to my mailtrap.

0 likes
14 replies
bobbybouwmann's avatar

So there might be two possibilities why this is going wrong.

  1. Are using the correct mail driver? You need to make sure your .env and mail.php are filled correctly.
  2. Right now your mail is using the Queueable trait. Do you have anything with queues in your application? If so you need to make sure to run php artisan queue:work to actually run all the queue tasks like emails or other jobs.
deladels's avatar

@bobbybouwmann yes i am. i configured everything. crosschecked it. i realised that when i i place or do this Mail::to($request->email)->send(new ScheduleBooked()); within the store function of the BookingsController it fires the email.

2.No please. i have nothing on queues set up at the moment.

Snapey's avatar

If you are not queuing then what is stopping you throwing a dd() into the mailable to see if it is being called?

have you checked that $request->email contains an email address?

deladels's avatar

@Snapey i actually did that. nothing is dumped. but like i said. putting the using the Mail facade directly in the store function sends an email.

Snapey's avatar

So you are not calling the email route?

deladels's avatar

and yes $request->email contains and email address

deladels's avatar

So you are not calling the email route? yeah. looks like i am not hitting/calling it.

Snapey's avatar

How are you expecting it to be hit?

Where do you call this method? It won't be reached unless a client does a get of /email

deladels's avatar

It'll be hit when the user clicks on the submit/book button

The method is called within the bookingcontroller class.

impbob36's avatar

I like to create preview routes for each of my mails, for visual confirmation: web.php:

Route::get('mail/welcome/{user}', ['as' => 'preview.welcomeMail', 'uses' => 'PreviewMailController@welcome']);

PreviewMailController:

use App\Http\Controllers\Controller;
use App\Mail\WelcomeMember;
use App\User;


class PreviewMailController extends Controller
{
    public function welcome(User $user)
    {
        return (new WelcomeMember($user));
    }
 ...
}
Snapey's avatar

The method is called within the bookingcontroller class.

Then why does it need a route?

@impbob - get your own thread...

1 like
deladels's avatar

@Snapey how do you suggest i go about it?

BookingsController.php

<?php

namespace App\Http\Controllers;

use App\Http\Controllers\Controllers;

use Illuminate\Http\Request;

use Braintree_Transaction;

use App\Booking;

use App\Schedule;

use Session;

use Mail;

use App\Mail\ScheduleBooked;

class BookingsController extends Controller
{ 

    public function store(Schedule $schedule, Request $request){

        $this->validate( request(), [

            'lastName'        => 'required',
            'firstName'       => 'required',
            'email'           => 'required',
            'mobilenumber'    => 'required',
            'additionalId'    => 'required|max:13',
            'idNumber'        => 'required'

        ]);

        Booking::create([

            'schedule_id'   => $schedule->id,
            'lastname'      => request('lastName'),
            'firstname'     => request('firstName'),
            'email'         => request('email'),
            'mobilenumber'  => request('mobilenumber'),
            'id_type'       => request('additionalId'),
            'id_number'     => request('idNumber')

        ]);

        $result = Braintree_Transaction::sale([
                      'amount' => $schedule->price,
                      'paymentMethodNonce' => $request->payment_method_nonce,
                      'customer' => [
                        'firstName'  => $request->firstName,
                        'lastName'   => $request->lastName,
                        'phone'      => $request->mobilenumber,
                        'email'      => $request->email
                      ],
                      'options' => [
                        'submitForSettlement' => true,
                      ]
        ]);

        //Mail::to($request->email)->send(new ScheduleBooked);

        if (!$result->success) {

            \Session::flash('flash_message', 'Your Booking was not Successfull, please try again or contact us for help');
            
            return redirect('uschedule/{schedule}');
        }
        
        \Session::flash('flash_message', 'Your Booking has been Successfull');

        return redirect('uschedules');
    }    



    public function email(Request $request, $bookingId){

        $booking = Booking::find($bookingId);

        Mail::to($request->email)->send(new ScheduleBooked($booking));

        return redirect('/uschedules');
    }
}

Snapey's avatar
Snapey
Best Answer
Level 122

What's wrong with this;

<?php

namespace App\Http\Controllers;

use App\Http\Controllers\Controllers;

use Illuminate\Http\Request;

use Braintree_Transaction;

use App\Booking;

use App\Schedule;

use Session;

use Mail;

use App\Mail\ScheduleBooked;

class BookingsController extends Controller
{ 

   public function store(Schedule $schedule, Request $request){

       $this->validate( request(), [

           'lastName'        => 'required',
           'firstName'       => 'required',
           'email'           => 'required',
           'mobilenumber'    => 'required',
           'additionalId'    => 'required|max:13',
           'idNumber'        => 'required'

       ]);

       $booking = Booking::create([                           // take note of the booking created

           'schedule_id'   => $schedule->id,
           'lastname'      => request('lastName'),
           'firstname'     => request('firstName'),
           'email'         => request('email'),
           'mobilenumber'  => request('mobilenumber'),
           'id_type'       => request('additionalId'),
           'id_number'     => request('idNumber')

       ]);

       $result = Braintree_Transaction::sale([
                     'amount' => $schedule->price,
                     'paymentMethodNonce' => $request->payment_method_nonce,
                     'customer' => [
                       'firstName'  => $request->firstName,
                       'lastName'   => $request->lastName,
                       'phone'      => $request->mobilenumber,
                       'email'      => $request->email
                     ],
                     'options' => [
                       'submitForSettlement' => true,
                     ]
       ]);


       if (!$result->success) {

           \Session::flash('flash_message', 'Your Booking was not Successfull, please try again or contact us for help');
           
           return redirect('uschedule/{schedule}');
       }
       
       // send the mail with the booking created earlier
       Mail::to($request->email)->send(new ScheduleBooked($booking));

       \Session::flash('flash_message', 'Your Booking has been Successfull');

       return redirect('uschedules');
   }    

}

2 likes

Please or to participate in this conversation.