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.
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:
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.
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
Method
Use Case
Performance
Mail::raw()
Quick test emails
Not scalable
Mailables
Reusable structured emails
Good for production
Queued Mailables
High-volume or bulk emails
Highly 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 article is a comprehensive tutorial on using the Prohibited Validation Rules in Laravel. You will learn how to apply prohibited, prohibited_if, and prohibited_unless with clear explanations, real-life scenarios, and code examples. This guide is perfect for developers who want to master advanced validation techniques in Laravel applications.
This detailed tutorial explores request validation in Laravel controllers. You’ll learn multiple techniques—basic controller validation, using form request classes, custom rules, conditional validation, error handling, localization, and best practices. With practical examples, code snippets, and structured explanations, this article is designed for beginners to advance learner.
This guide teaches you how to deploy Laravel applications to production servers. From preparing your environment and configuring Nginx or Apache, to database migrations, caching, performance optimization, CI/CD pipelines, and security practices—this article covers everything step by step.It’s suitable for both beginners and advanced developers who want to ship stable, secure & scalable app.
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
These cookies are essential for the website to function properly.
These cookies help us understand how visitors interact with the website.
These cookies are used to deliver personalized advertisements.