Laravel 12 Send Email Tutorial: Step-by-Step Guide

In this tutorial, I will show you how to send emails in Laravel 12 using the built-in Mail feature. Sending emails is an essential feature in web applications, whether for user notifications, password resets, or contact forms.

We will go through the step-by-step process, including configuring the mail server, creating a Mailable class, and sending emails using Laravel’s Mail facade.

Step-by-Step Guide to Sending Email in Laravel 12

Laravel 12 Send Email Tutorial: Step-by-Step Guide

 

Step 1: Install Laravel 12

If you haven’t installed Laravel 12 yet, run the following command:

laravel new laravel-12-send-email

 

Step 2: Configure Mail Settings

Open the .env file and update the email configuration:

MAIL_MAILER=smtp
MAIL_HOST=smtp.gmail.com
MAIL_PORT=587
[email protected]
MAIL_PASSWORD=your-email-password
MAIL_ENCRYPTION=tls
[email protected]
MAIL_FROM_NAME="Your App Name"

Note: If you are using Gmail SMTP, you may need to enable Less Secure Apps or generate an App Password for authentication.

 

Step 3: Create Mailable Class

Run the following command to generate a new Mailable class:

php artisan make:mail SendEmail

This will create a file in app/Mail/SendEmail.php.

 

Step 4: Define Email Content

Open app/Mail/SendEmail.php and update it:

<?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 SendEmail extends Mailable
{
    use Queueable, SerializesModels;

    public $data;
    
    /**
     * Create a new message instance.
     */
    public function __construct($data)
    {
        $this->data = $data;
    }

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

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

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

 

Step 5: Create Email Blade Template

Create a new directory resources/views/emails/ and inside it, create a file send-email.blade.php:

<!DOCTYPE html>
<html>
<head>
    <title>Email from Laravel 12</title>
</head>
<body>
    <h2>Hello, {{ $data['name'] }}!</h2>
    <p>{{ $data['message'] }}</p>
    <p>Thank you!</p>
</body>
</html>

 

Step 6: Create a Controller for Sending Emails

Run the following command to create a new controller:

php artisan make:controller EmailController

Now open app/Http/Controllers/EmailController.php and update it:

namespace App\Http\Controllers;

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

class EmailController extends Controller
{
    public function sendEmail()
    {
        $data = [
            'name' => 'John Doe',
            'message' => 'This is a test email from Laravel 12.'
        ];

        Mail::to('[email protected]')->send(new SendEmail($data));

        return response()->json(['success' => 'Email sent successfully.']);
    }
}

 

Step 7: Define Route for Sending Emails

Open routes/web.php and add this route:

use App\Http\Controllers\EmailController;

Route::get('/send-email', [EmailController::class, 'sendEmail']);

 

Step 8: Send a Test Email

Now, start your Laravel server:

php artisan serve

 


You might also like:

techsolutionstuff

Techsolutionstuff | The Complete Guide

I'm a software engineer and the founder of techsolutionstuff.com. Hailing from India, I craft articles, tutorials, tricks, and tips to aid developers. Explore Laravel, PHP, MySQL, jQuery, Bootstrap, Node.js, Vue.js, and AngularJS in our tech stack.

RECOMMENDED POSTS

FEATURE POSTS