• Fri, Mar 2026

Send Emails in Laravel with Mailables and Queues

Send Emails in Laravel with Mailables and Queues

This comprehensive guide explores how to send emails in Laravel using Mailables and Queues. You’ll learn everything from setting up email configuration, creating mailables, attaching files, customizing templates, and handling large-scale email sending with queues. Perfect for beginners who want to master Laravel’s email system step by step.

Introduction

Email is one of the most powerful way to communicate with users, whether it’s sending password, reset links, order confirmations, or promotional campaigns. In Laravel, sending emails is not just simple but also scalable and flexible, thanks to its robust integration with Mailables and Queues.

In this tutorial, we’ll walk you through everything you need to know about sending emails in Laravel. You’ll start with the basics, like configuring your email settings, and then move toward building reusable Mailable classes, crafting Blade-based email templates, and leveraging Laravel Queues for efficient background email sending.

Suggested Visual: A diagram showing the flow: User Action → Controller → Mailable → Mail Driver → Inbox.

Step 1: Configuring Email in Laravel

Before you send any email, you need to configure Laravel’s email settings. Laravel supports multiple drivers such as SMTP, Mailgun, Postmark, SES, and more.

Setting up .env Configuration

Open your project’s .env file and configure the following settings:

MAIL_MAILER=smtp
MAIL_HOST=smtp.mailtrap.io
MAIL_PORT=2525
MAIL_USERNAME=your_mailtrap_username
MAIL_PASSWORD=your_mailtrap_password
MAIL_ENCRYPTION=tls
MAIL_FROM_ADDRESS=example@yourdomain.com
MAIL_FROM_NAME="My Laravel App"

We’re using Mailtrap for testing since it provides a sandbox environment for capturing emails during development.

Step 2: Sending a Simple Test Email

Laravel provides a Mail facade to quickly send raw emails. You can test this in your routes/web.php file:


use Illuminate\Support\Facades\Mail;

Route::get('/send-test', function () {
    Mail::raw('This is a test email from Laravel!', function ($message) {
        $message->to('user@example.com')
                ->subject('Test Email');
    });

    return 'Test email sent!';
});

When you visit http://localhost:8000/send-test, Laravel sends the email using your configured mailer.

Step 3: Creating a Mailable Class

While Mail::raw works for quick tests, Laravel encourages using Mailables for structured and reusable email sending.

Run the following Artisan command to create a Mailable:

php artisan make:mail WelcomeMail

This generates a file in app/Mail/WelcomeMail.php.

Editing the Mailable


namespace App\Mail;

use Illuminate\Bus\Queueable;
use Illuminate\Mail\Mailable;
use Illuminate\Queue\SerializesModels;

class WelcomeMail extends Mailable
{
    use Queueable, SerializesModels;

    public $user;

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

    public function build()
    {
        return $this->subject('Welcome to My Laravel App')
                    ->view('emails.welcome');
    }
}

We’re passing a $user object so that the email can be personalized.

Step 4: Creating an Email Template with Blade

Create a new Blade template at resources/views/emails/welcome.blade.php:

<!DOCTYPE html>
<html>
<head>
    <title>Welcome Email</title>
</head>
<body>
    <h1>Hello, {{ $user->name }}!</h1>
    <p>Thank you for joining our Laravel app. We’re excited to have you!</p>
</body>
</html>

Step 5: Triggering the Email

You can now send the email from a controller:


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

public function registerUser(Request $request)
{
    $user = User::create($request->all());

    Mail::to($user->email)->send(new WelcomeMail($user));

    return "User registered and welcome email sent!";
}

Step 6: Attaching Files

You can attach files inside the build() method of your Mailable:


public function build()
{
    return $this->subject('Invoice for Your Purchase')
                ->view('emails.invoice')
                ->attach(storage_path('invoices/invoice.pdf'));
}

Step 7: Using Markdown Mailables

Laravel also supports Markdown-based emails, which allow quick styling.

php artisan make:mail OrderShipped --markdown=emails.orders.shipped

This generates a Markdown template at resources/views/emails/orders/shipped.blade.php with pre-built components like buttons.

Step 8: Sending Emails with Queues

If your app sends many emails (like newsletters), sending them synchronously can slow things down. That’s where Laravel Queues shine.

Configuring Queue Driver

Update .env to use database driver:

QUEUE_CONNECTION=database

Run migrations for jobs:

php artisan queue:table
php artisan migrate

Queueing the Mailable


Mail::to($user->email)->queue(new WelcomeMail($user));

Now, instead of sending immediately, Laravel pushes the job into the queue. Run the worker with:

php artisan queue:work

Step 9: Delayed Emails

You can delay sending emails by specifying a time:


Mail::to($user->email)->later(now()->addMinutes(10), new WelcomeMail($user));

Step 10: Error Handling & Failed Jobs

If an email fails to send, Laravel stores it in the failed_jobs table.

php artisan queue:failed

You can retry failed jobs with:

php artisan queue:retry all

Best Practices for Sending Emails in Laravel

  • Always use environment variables for credentials.
  • Queue large-scale email jobs.
  • Use Markdown mailables for clean and responsive emails.
  • Log all outgoing emails for auditing.
  • Test in a sandbox environment like Mailtrap before production.

Comparison of Email Sending Methods

MethodUse CasePerformance
Mail::raw()Quick test emailsNot scalable
MailablesReusable structured emailsGood for production
Queued MailablesHigh-volume or bulk emailsHighly scalable

Conclusion

Sending emails in Laravel is not only easy but also powerful when using Mailables and Queues. From simple messages to highly scalable newsletters, Laravel gives you all the tools you need to manage email workflows effectively. By mastering Mailables, Blade templates, attachments, and queues, you can build professional-grade email systems that enhance user engagement and reliability.

This website uses cookies to enhance your browsing experience. By continuing to use this site, you consent to the use of cookies. Please review our Privacy Policy for more information on how we handle your data. Cookie Policy