
Introduction to Laravel Mailables
Sending emails in Laravel has never been easier thanks to Mailables. Mailables are classes that represent individual emails and provide a clean, object-oriented way to build and send email messages. Instead of dealing with raw strings and headers, you can create structured, testable email classes with Laravel's powerful mailable system.
Setting Up Email Configuration
Before sending emails, configure your mail settings in .env file:
MAIL_MAILER=smtp
MAIL_HOST=smtp.mailtrap.io
MAIL_PORT=2525
MAIL_USERNAME=your_username
MAIL_PASSWORD=your_password
MAIL_ENCRYPTION=tls
MAIL_FROM_ADDRESS="[email protected]"
MAIL_FROM_NAME="${APP_NAME}"
// Alternative: SendGrid
MAIL_MAILER=smtp
MAIL_HOST=smtp.sendgrid.net
MAIL_PORT=587
MAIL_USERNAME=apikey
MAIL_PASSWORD=your_sendgrid_api_key
MAIL_ENCRYPTION=tls
Creating Your First Mailable
Let's create a basic welcome email mailable:
php artisan make:mail WelcomeEmail
Now implement the mailable class:
// app/Mail/WelcomeEmail.php
namespace App\Mail;
use Illuminate\Bus\Queueable;
use Illuminate\Mail\Mailable;
use Illuminate\Queue\SerializesModels;
class WelcomeEmail extends Mailable
{
use Queueable, SerializesModels;
public $user;
public $password;
public function __construct($user, $password)
{
$this->user = $user;
$this->password = $password;
}
public function build(): self
{
return $this->subject('Welcome to Our Platform!')
->view('emails.welcome')
->with([
'userName' => $this->user->name,
'userEmail' => $this->user->email,
'password' => $this->password,
]);
}
}
Creating Email Views
Create the email view template:
// resources/views/emails/welcome.blade.php
@component('mail::message')
# Welcome to {{ config('app.name') }}
Hello {{ $userName }},
Thank you for joining our platform! Your account has been successfully created.
## Account Details
**Email:** {{ $userEmail }}
**Password:** {{ $password }}
## Getting Started
1. **Login to your account:** [Login Here]({{ config('app.url') }}/login)
2. **Complete your profile:** Add your avatar and personal information
3. **Explore features:** Check out all the amazing tools we offer
If you have any questions, feel free to reach out to our support team.
Best regards,
{{ config('app.name') }} Team
@component('mail::button', ['url' => config('app.url'), 'color' => 'blue'])
Visit Our Website
@endcomponent
Thanks,
{{ config('app.name') }}
@endcomponent
Sending Mailables
Basic Email Sending:
use App\Mail\WelcomeEmail;
use Illuminate\Support\Facades\Mail;
// Send immediately
Mail::to($user->email)->send(new WelcomeEmail($user, $password));
// Send to multiple recipients
Mail::to([$user1->email, $user2->email])
->cc(['[email protected]'])
->bcc(['[email protected]'])
->send(new WelcomeEmail($user1, $password));
Queued Email Sending
For better performance, queue emails:
// Queue email
Mail::to($user->email)->queue(new WelcomeEmail($user, $password));
// Queue with delay
Mail::to($user->email)->later(now()->addMinutes(10), new WelcomeEmail($user, $password));
// Queue with specific queue
Mail::to($user->email)->onQueue('emails')->queue(new WelcomeEmail($user, $password));
SMTP Configuration
Google SMTP:
MAIL_MAILER=smtp
MAIL_HOST=smtp.gmail.com
MAIL_PORT=587
[email protected]
MAIL_PASSWORD=your-app-password
MAIL_ENCRYPTION=tls
[email protected]
MAIL_FROM_NAME="Your App Name"
Custom SMTP:
MAIL_MAILER=smtp
MAIL_HOST=your-smtp-server.com
MAIL_PORT=25
MAIL_USERNAME=your-username
MAIL_PASSWORD=your-password
MAIL_ENCRYPTION=
[email protected]
MAIL_FROM_NAME="Your App Name"
Conclusion
Laravel Mailables provide a powerful, flexible way to send emails in your applications. By following this tutorial, you've learned how to create mailables, design email templates, send emails, and handle different email scenarios.
Remember to always test your emails thoroughly, use queues for better performance, and implement proper error handling. Email communication is crucial for user engagement, so make sure your emails are professional, clear, and well-designed.
Happy sending emails with Laravel!