Prompt
How do I send emails from a Laravel app?
Latest observation
To send emails from a Laravel app, you typically use Laravel’s built-in Mail system, which sits on top of Symfony Mailer.
1) Configure your mail driver
Set your mail settings in .env:
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=no-reply@example.com
MAIL_FROM_NAME="${APP_NAME}"
Common options:
smtp— most commonmailgun,postmark,ses— for email serviceslog— writes emails to logs instead of sendingarray— stores emails in memory for testing
2) Create a Mailable
Generate a mailable class:
php artisan make:mail WelcomeMail
This creates a class in app/Mail/WelcomeMail.php.
Example:
use Illuminate\Mail\Mailable;
class WelcomeMail extends Mailable
{
public function build()
{
return $this->subject('Welcome!')
->view('emails.welcome');
}
}
3) Create the email view
Create a Blade template:
resources/views/emails/welcome.blade.php
<h1>Welcome, {{ $name }}!</h1>
<p>Thanks for joining us.</p>
If you pass data to the mailable, you can display it in the view.
Example mailable with data:
class WelcomeMail extends Mailable
{
public function __construct(public $name) {}
public function build()
{
return $this->subject('Welcome!')
->view('emails.welcome');
}
}
4) Send the email
In a controller, route, or job:
use Illuminate\Support\Facades\Mail;
use App\Mail\WelcomeMail;
Mail::to('user@example.com')->send(new WelcomeMail('John'));
5) Queue emails if needed
For better performance, you can queue emails:
Mail::to('user@example.com')->queue(new WelcomeMail('John'));
Your mailable should implement ShouldQueue if you want it queued automatically:
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Mail\Mailable;
class WelcomeMail extends Mailable implements ShouldQueue
{
// ...
}
Then make sure your queue is configured and a worker is running.
6) Test email sending
For local development, a tool like:
- Mailtrap
- Mailpit
- Mailhog
is useful so you can inspect messages without sending real emails.
If you want, I can also show you:
- a complete Laravel 11 example,
- how to send HTML emails, or
- how to use queue jobs for email sending.