# 📧 Sending Emails with Mail Facade in Laravel
Sending emails is a common task in web applications — whether it’s for user registration, password reset, or notifications. In Laravel, the **Mail facade** makes this super simple.
---
## 🔧 Step 1: Configure Mail Settings
Open your `.env` file and set up your mail service:
```env
MAIL_MAILER=smtp
MAIL_HOST=smtp.mailtrap.io
MAIL_PORT=2525
MAIL_USERNAME=your_username
MAIL_PASSWORD=your_password
MAIL_ENCRYPTION=tls
[email protected]
MAIL_FROM_NAME="MyApp"
📝 Step 2: Create a Mailable
Run this Artisan command:
php artisan make:mail WelcomeMail
This creates app/Mail/WelcomeMail.php. Inside it, you can define how the email should look. Example:
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 MyApp')
->view('emails.welcome');
}
}
🎨 Step 3: Create Email Blade View
Make a new file: resources/views/emails/welcome.blade.php
<!DOCTYPE html>
<html>
<head>
<title>Welcome</title>
</head>
<body>
<h2>Welcome, {{ $user->name }}!</h2>
<p>Thank you for joining MyApp. We're excited to have you onboard.</p>
</body>
</html>
🚀 Step 4: Send the Email
Use the Mail facade in your controller or route:
use App\Mail\WelcomeMail;
use Illuminate\Support\Facades\Mail;
Route::get('/send-mail', function () {
$user = (object) ['name' => 'John Doe', 'email' => '[email protected]'];
Mail::to($user->email)->send(new WelcomeMail($user));
return "Email has been sent!";
});
🎯 Done!
That’s it! You’ve successfully sent an email using Laravel’s Mail facade.
💡 Pro Tip:
Use queue() instead of send() for background sending:
Mail::to($user->email)->queue(new WelcomeMail($user));