Issue using Mailable and markdowns

Published 7 months ago by deladels

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', '[email protected]')->name('sendEmail');

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

Best Answer (As Selected By deladels)
Snapey

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');
   }    

}

bobbybouwmann

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

@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
Snapey
7 months ago (959,365 XP)

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

@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
Snapey
7 months ago (959,365 XP)

So you are not calling the email route?

deladels

and yes $request->email contains and email address

deladels

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

Snapey
Snapey
7 months ago (959,365 XP)

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

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

The method is called within the bookingcontroller class.

impbob

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' => '[email protected]']);

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
Snapey
7 months ago (959,365 XP)

The method is called within the bookingcontroller class.

Then why does it need a route?

@impbob - get your own thread...

deladels

@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
Snapey
7 months ago (959,365 XP)

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');
   }    

}

deladels

@Snapey thanks so much for your help. it works just fine. :)

Please sign in or create an account to participate in this conversation.