Laravel 12 Email Sending: A Complete Step-by-Step Guide

Author

Kritim Yantra

Mar 26, 2025

Laravel 12 Email Sending: A Complete Step-by-Step Guide

Sending emails in Laravel is a common requirement for web applications, whether for user registration, password resets, or notifications. Laravel provides a clean, simple email API powered by Symfony Mailer, making it easy to send emails via SMTP, Mailgun, Postmark, and more.

In this guide, we’ll walk through how to send emails in Laravel 12 with detailed steps and best practices.


Prerequisites

Before we start, ensure you have:

  • Laravel 12 installed (or a newer version)
  • A configured email service (Gmail, Mailtrap, SendGrid, etc.)
  • Basic knowledge of Laravel MVC structure

Step 1: Configure Email Settings in Laravel

Laravel’s email configuration is stored in the .env file. Open it and update the following settings:

MAIL_MAILER=smtp
MAIL_HOST=smtp.gmail.com
MAIL_PORT=587
MAIL_USERNAME=your@gmail.com
MAIL_PASSWORD=your_app_password
MAIL_ENCRYPTION=tls
MAIL_FROM_ADDRESS="your@gmail.com"
MAIL_FROM_NAME="${APP_NAME}"

Notes:

  • If using Gmail, enable "Less Secure Apps" or generate an App Password.
  • For testing, use Mailtrap (a fake SMTP service):
    MAIL_MAILER=smtp
    MAIL_HOST=sandbox.smtp.mailtrap.io
    MAIL_PORT=2525
    MAIL_USERNAME=your_mailtrap_username
    MAIL_PASSWORD=your_mailtrap_password
    MAIL_ENCRYPTION=tls
    

Step 2: Create a Mailable Class

Laravel uses "Mailable" classes to build emails. Generate one using:

php artisan make:mail WelcomeEmail

This creates app/Mail/WelcomeEmail.php. Open it and modify:

<?php

namespace App\Mail;

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

class WelcomeEmail extends Mailable
{
    use Queueable, SerializesModels;

    public $user;

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

    public function envelope(): Envelope
    {
        return new Envelope(
            subject: 'Welcome to Our Application',
        );
    }

    public function content(): Content
    {
        return new Content(
            view: 'emails.welcome',
        );
    }
    /**
     * Get the attachments for the message.
     *
     * @return array
     */
    public function attachments(): array
    {
        return [];
    }
}

Step 3: Create an Email Blade Template

Laravel emails use Blade templates. Create a new file at resources/views/emails/welcome.blade.php:

<!DOCTYPE html>
<html>
<head>
    <title>Welcome Email</title>
</head>
<body>
    <h1>Welcome, {{ $user->name }}!</h1>
    <p>Thank you for joining our application.</p>
    <p>Start exploring now.</p>
</body>
</html>

Step 4: Send the Email

Now, trigger the email from a controller, route, or event. Here’s an example in a controller:

Option 1: Using a Controller

use App\Mail\WelcomeEmail;
use Illuminate\Support\Facades\Mail;

public function sendWelcomeEmail()
{
    $user = auth()->user(); // Or fetch a user from DB
    Mail::to($user->email)->send(new WelcomeEmail($user));
    return "Email sent successfully!";
}

Option 2: Using a Route Closure

use App\Mail\WelcomeEmail;
use Illuminate\Support\Facades\Mail;
use Illuminate\Support\Facades\Route;

Route::get('/send-email', function () {
    $user = ['name' => 'John Doe', 'email' => 'john@example.com'];
    Mail::to($user['email'])->send(new WelcomeEmail((object)$user));
    return "Email sent!";
});

Step 5: Testing Email Sending

To test emails without sending real ones, use Laravel’s log driver (stores emails in storage/logs/laravel.log):

MAIL_MAILER=log

Or use Mailtrap for a fake inbox.


Step 6: Queueing Emails (Optional)

To improve performance, send emails via queues:

  1. Configure a queue driver (Redis, database, etc.) in .env:

    QUEUE_CONNECTION=database
    
  2. Generate a migration for jobs:

    php artisan queue:table
    php artisan migrate
    
  3. Modify the WelcomeEmail class to implement ShouldQueue:

    use Illuminate\Contracts\Queue\ShouldQueue;
    
    class WelcomeEmail extends Mailable implements ShouldQueue
    
  4. Dispatch the email:

    Mail::to($user->email)->queue(new WelcomeEmail($user));
    
  5. Run the queue worker:

    php artisan queue:work
    

Conclusion

Sending emails in Laravel 12 is straightforward with Mailable classes, Blade templates, and SMTP configurations. You can:

  • Customize emails with dynamic data.
  • Test emails using Mailtrap or log driver.
  • Improve performance with queues.

Now you’re ready to integrate emails into your Laravel application seamlessly! 🚀

Let me know if you need any clarifications! Happy coding! 😊

Tags

Laravel Php

Comments

No comments yet. Be the first to comment!

Please log in to post a comment:

Continue with Google

Related Posts

Understanding SOLID Design Principles in Laravel (For Beginners)
Kritim Yantra Kritim Yantra
Feb 24, 2025
Laravel 12 Roles and Permissions Setup: Complete Guide
Kritim Yantra Kritim Yantra
Feb 28, 2025
Laravel 12 & AdminLTE Integration: Setup Your Stunning Admin Dashboard
Kritim Yantra Kritim Yantra
Feb 28, 2025