How to Configure Mail in Laravel: Setup and Example
To configure mail in Laravel, set your mail driver and credentials in the
.env file using keys like MAIL_MAILER, MAIL_HOST, and MAIL_USERNAME. Then, use Laravel's Mail facade to send emails based on this configuration.Syntax
Laravel uses environment variables in the .env file to configure mail settings. The main keys are:
MAIL_MAILER: The mail driver (e.g., smtp, sendmail, log).MAIL_HOST: The SMTP server address.MAIL_PORT: The SMTP port number.MAIL_USERNAMEandMAIL_PASSWORD: Credentials for authentication.MAIL_ENCRYPTION: Encryption method (tls or ssl).MAIL_FROM_ADDRESSandMAIL_FROM_NAME: Default sender email and name.
Laravel reads these settings automatically to send emails.
env
MAIL_MAILER=smtp MAIL_HOST=smtp.mailtrap.io MAIL_PORT=2525 MAIL_USERNAME=null MAIL_PASSWORD=null MAIL_ENCRYPTION=tls MAIL_FROM_ADDRESS=example@example.com MAIL_FROM_NAME="Example App"
Example
This example shows how to send a simple email using Laravel's Mail facade with the configured SMTP settings.
php
<?php use Illuminate\Support\Facades\Mail; Route::get('/send-mail', function () { $details = [ 'title' => 'Test Email from Laravel', 'body' => 'This is a test email sent using Laravel mail configuration.' ]; Mail::raw($details['body'], function ($message) use ($details) { $message->to('recipient@example.com') ->subject($details['title']); }); return 'Email sent successfully!'; });
Output
Email sent successfully!
Common Pitfalls
Common mistakes when configuring mail in Laravel include:
- Not setting correct SMTP credentials in
.env, causing authentication failures. - Forgetting to clear config cache after changing
.envwithphp artisan config:clear. - Using wrong mail driver or port for your mail service.
- Not setting
MAIL_FROM_ADDRESSandMAIL_FROM_NAME, which can cause emails to be rejected.
Always verify your SMTP details and clear config cache after changes.
php
/* Wrong way: Not clearing config cache after .env change */ // Changed MAIL_USERNAME in .env but did not run: // php artisan config:cache /* Right way: */ // After editing .env, run: // php artisan config:cache // to reload mail settings
Quick Reference
| Setting | Description | Example Value |
|---|---|---|
| MAIL_MAILER | Mail driver to use | smtp |
| MAIL_HOST | SMTP server address | smtp.mailtrap.io |
| MAIL_PORT | SMTP server port | 2525 |
| MAIL_USERNAME | SMTP username | your_username |
| MAIL_PASSWORD | SMTP password | your_password |
| MAIL_ENCRYPTION | Encryption method | tls |
| MAIL_FROM_ADDRESS | Default sender email | example@example.com |
| MAIL_FROM_NAME | Default sender name | "Example App" |
Key Takeaways
Set mail configuration in the .env file using correct SMTP details.
Always clear Laravel config cache after changing mail settings with php artisan config:clear.
Use Laravel's Mail facade to send emails based on your configuration.
Ensure MAIL_FROM_ADDRESS and MAIL_FROM_NAME are set to avoid email rejection.
Test mail sending with a simple route or command to verify configuration.