Laravel makes email sending simple using its built-in Mail
facade and Mailable classes. You can configure your SMTP settings in the .env
file and create reusable email templates using Blade views. The php artisan make:mail
command generates a mailable class for organizing email logic. Emails can be sent via various drivers like SMTP, Mailgun, or Sendmail. Laravel also supports queuing and markdown-based email formatting for advanced use cases.
Step 1: Configure Mail Settings in .env
MAIL_MAILER=smtp
MAIL_HOST=smtp.gmail.com
MAIL_PORT=587
MAIL_USERNAME=your_email@gmail.com
MAIL_PASSWORD=your_email_password
MAIL_ENCRYPTION=tls
MAIL_FROM_ADDRESS=your_email@gmail.com
MAIL_FROM_NAME="${APP_NAME}"
If using Gmail, enable “Less Secure Apps” or use an App Password with 2FA.
Step 2: Create a Mailable Class
Put this comment in your project terminal:
php artisan make:mail WelcomeMail
This creates app/Mail/WelcomeMail.php
Step 3: Define Email Content in Mailable
In WelcomeMail.php
:
use Illuminate\Mail\Mailable;
class WelcomeMail extends Mailable
{
public $user;
public function __construct($user)
{
$this->user = $user;
}
public function build()
{
return $this->view('emails.welcome')
->with(['user' => $this->user]);
}
}
Step 4: Create the Email View
Create a Blade file at:resources/views/emails/welcome.blade.php
<h1>Hi {{ $user['name'] }},</h1>
Welcome to our platform!
Step 5: Send the Email from Controller
Edit something like app/Http/Controllers/UserController.php
or HomeController.php
:
use Illuminate\Support\Facades\Mail;
use App\Mail\WelcomeMail;
public function sendEmail()
{
$user = ['name' => 'John', 'email' => 'john@example.com'];
Mail::to($user['email'])->send(new WelcomeMail($user));
return "Email Sent!";
}
Result:
This will send an email with the content from your Blade view to the specified user.