Kritim Yantra
Mar 26, 2025
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.
Before we start, ensure you have:
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}"
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
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 [];
}
}
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>
Now, trigger the email from a controller, route, or event. Here’s an example in 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!";
}
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!";
});
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.
To improve performance, send emails via queues:
Configure a queue driver (Redis, database, etc.) in .env
:
QUEUE_CONNECTION=database
Generate a migration for jobs:
php artisan queue:table
php artisan migrate
Modify the WelcomeEmail
class to implement ShouldQueue
:
use Illuminate\Contracts\Queue\ShouldQueue;
class WelcomeEmail extends Mailable implements ShouldQueue
Dispatch the email:
Mail::to($user->email)->queue(new WelcomeEmail($user));
Run the queue worker:
php artisan queue:work
Sending emails in Laravel 12 is straightforward with Mailable classes, Blade templates, and SMTP configurations. You can:
Now you’re ready to integrate emails into your Laravel application seamlessly! 🚀
Let me know if you need any clarifications! Happy coding! 😊
No comments yet. Be the first to comment!
Please log in to post a comment:
Continue with Google