Laravel sends email through Mailable classes — one class per kind of email (welcome, invoice, password reset).
Configure SMTP in .env
Set MAIL_MAILER=smtp, MAIL_HOST, MAIL_PORT, MAIL_USERNAME, MAIL_PASSWORD, MAIL_FROM_ADDRESS and MAIL_FROM_NAME using your email provider's details (Gmail app password, Hostinger mail, Mailgun, SES…).
While developing, use MAIL_MAILER=log: emails are written to storage/logs/laravel.log instead of being sent.
Create a mailable
php artisan make:mail WelcomeMail creates app/Mail/WelcomeMail.php with:
envelope()— the subjectcontent()— the Blade view used for the bodyattachments()— files to attach
Public properties of the class are available in the view automatically.
Send it
Mail::to($user)->send(new WelcomeMail($user));
Sending through SMTP takes a few seconds. Use ->queue(...) instead of ->send(...) to send it in the background (see the Queues lesson).
Example
<?php
// app/Mail/WelcomeMail.php
namespace App\Mail;
use App\Models\User;
use Illuminate\Mail\Mailable;
use Illuminate\Mail\Mailables\Content;
use Illuminate\Mail\Mailables\Envelope;
class WelcomeMail extends Mailable
{
public function __construct(public User $user) {}
public function envelope(): Envelope
{
return new Envelope(subject: 'Welcome to AgyanAdda!');
}
public function content(): Content
{
return new Content(view: 'emails.welcome');
}
}
// resources/views/emails/welcome.blade.php
// <h2>Hi {{ $user->name }},</h2>
// <p>Thanks for joining. Start learning today!</p>
// Controller
use Illuminate\Support\Facades\Mail;
Mail::to($user)->send(new WelcomeMail($user));
The user receives an email with the subject "Welcome to AgyanAdda!" and the body "Hi Asha, Thanks for joining...". With MAIL_MAILER=log it appears in storage/logs/laravel.log instead.