Create a Contact Form in Laravel 10

 

For our local environment we will configure Mailtrap.

Mailtrap is an easy to use email sandbox to inspect and debug emails in staging, dev, and QA environments. You can test email before deploying to production.

MAIL_MAILER=smtp
MAIL_HOST=smtp.mailtrap.io
MAIL_PORT=your_mailtrap_port
MAIL_USERNAME=your_mailtrap_key
MAIL_PASSWORD=your_mailtrap_key
MAIL_ENCRYPTION=tls
MAIL_FROM_ADDRESS=handle@example.com
MAIL_FROM_NAME="${APP_NAME}"

 

For our product environment we suggest using SendGrid.

SendGrid is a cloud-based SMTP provider that allows you to send email without having to maintain email servers.

 

Create the Controller

After setting the routes, I will create a controller to handle the requests from the routes.

php artisan make:controller ContactController

This command will create a file, app/Http/Controllers/ContactController.php

We will need to create two methods in this controller. One to handle the GET request and one to handle the POST request.

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\Mail;
use App\Mail\ContactUs;


class ContactController extends Controller
{
    public function send(Request $request)
    {

        //validate the inputs
        $request->validate([
            'email' => 'required',
            'message' => 'required',
        ]);
        
        //put request data in an array
        $data = [
        	'from' => $request->email,
            'message' => $request->message
        ];
        
        //send email to site owner via a Mailable
        Mail::to({{email})->send(new Contact($data));

        return redirect('/')->with(['status' => 'Email has been Sent']);
    }
    
    public function page(Request $request)
    {
        return view('pages.contact');
    }
  }

Create Mailable

php artisan make:mail Contact

This willl create a file app/Mail/Contact.php and generated a mailable class Contact

<?php

namespace App\Mail;

use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Mail\Mailable;
use Illuminate\Mail\Mailables\Content;
use Illuminate\Mail\Mailables\Envelope;
use Illuminate\Queue\SerializesModels;

class Contact extends Mailable
{
    use Queueable, SerializesModels;

    /**
     * Create a new message instance.
     */
    public function __construct()
    {
        //
    }

    /**
     * Get the message envelope.
     */
    public function envelope(): Envelope
    {
        return new Envelope(
            subject: 'Contact',
        );
    }

    /**
     * Get the message content definition.
     */
    public function content(): Content
    {
        return new Content(
            view: 'emails.contact',
        );
    }

    /**
     * Get the attachments for the message.
     *
     * @return array<int, \Illuminate\Mail\Mailables\Attachment>
     */
    public function attachments(): array
    {
        return [];
    }
}

Create the Routes

Go to routes/web.php and add the following code. One route will be a GET request that will display the contact form. The other route will be the post route for the form submission.

use App\Http\Controllers\ContactController;
Route::get('contact', [ContactController::class, 'page'])->name('pages.contact');
Route::post('contact', [ContactController::class, 'send'])->name('send.contact')

 

Create the Views

The page() method returns the Blade view that displays the contact HTML form.