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

Yomamen's avatar

How to send mail from a custom form ?

Hey guys, i'm currently trying to do a personal project where people can book a talent by filling in form and the form data will pass on to email's content and send it to the talent's email. How do i pass the data to the email ?

This is what i currently have (i've already setup Mailtrap)

bookingEnquiryMail Controller

public function bookingEnquiryMail(Request $request, User $user) {
    $data = array(
        'bookingName' => $request->name,
        'bookingLocation' => $request->location,
        'bookingDate' => $request->date,
        'bookingNote' => $request->note,
        'bookingAmount' => $request->amount
    );
    Mail::send('emails.booking-enquiry', $data, function($message) use ($data, $user){
        $message->from('[email protected]');
        $message->to($user->email);
        $message->subject('test');
    });
    
    return redirect('/talent')
}

booking-form blade template

<form method="POST" action="/talent/{{$user->id}}">
    <input type="hidden" name="_token" value="{{{ csrf_token() }}}">
    <div class="form-group">
        <label for="bookingName">Representative Name</label>
        <input type="string" name="bookingName" class="form-control" value="">
    </div>
    <div class="form-group">
        <label for="bookingLocation">Location</label>
        <input type="string" name="bookingLocation" class="form-control" value="">
    </div>
    <div class="form-group">
        <label for="bookingDate">Date</label>
        <input type="date" class="form-control" name="bookingDate">
    </div>
    <div class="form-group">
        <label for="bookingNote">Add a Note</label>
        <input type="string" class="form-control" name="bookingNote">
    </div>
    <div class="form-group">
        <label for="bookingAmount">Offer Amount</label>
        <input type="string" class="form-control" name="bookingAmount">
    </div>
    <button type="submit" class="btn btn-primary">Send Booking Enquiry</button>
</form>

supposed to be email content template

<p>{{$bookingName}}</p>
<p>{{$bookingLocation}}</p>
<p>{{$bookingDate}}</p>
<p>{{$bookingNote}}</p>
<p>{{$bookingAmount}}</p>

I just dont know how to do mail stuffs in general, and the examples i have seen seem to have different structure so it makes me confused

0 likes
6 replies
tisuchi's avatar

Which laravel version you are using?

Yomamen's avatar

I kinda get the idea of mails now, here is my updated code:

bookingEnquiryController

public function bookingEnquiryMail(Request $request, User $user) {
    
    $data = array(
        'bookingName' => $request->bookingName,
        'bookingLocation' => $request->bookingLocation,
        'bookingDate' => $request->bookingDate,
        'bookingNote' => $request->bookingNote,
        'bookingAmount' => $request->bookingAmount
    );
    Mail::send('emails.booking-enquiry', compact('request', 'user', 'data'), function($message) use ($request, $user, $data){
        $message->from('[email protected]');
        $message->to($user->email);
        $message->subject('Booking Enquiry');
    });
    
    return redirect('/talent');
}

emails.template

<p>{{ $bookingName }}</p>

However, when i declare $bookingName Laravel returns "Undefined variable: bookingName". From what i see in the controller i have declared bookingName right ? i've tried

dd($request->bookingName);

and it returns the input value, im not sure what is going on.

tisuchi's avatar

@Yomamen

I don't think that you can call {{ $bookingName }} directly since you are passing as an array.

May be this is the way-

{{ $data['bookingName'] }}

Please or to participate in this conversation.