Laravel mail to log file, instead of actually being sent. The Laravel email log file is stored in storage/logs/laravel.log file.
Laravel Mail Log Example:
How To Enable Email Log From Laravel?
Edit .env file MAIL_MAILER
and LOG_CHANNEL
variable. Then clear the cache and start testing.
LOG_CHANNEL=stack
# No need to change other variables
MAIL_MAILER=log
Give correct permission to the storage folder. sudo chmod -R 777 /path-to/storage/
Clear Config Cache From Laravel
You can clear the Laravel config cache by terminal command.
php artisan config:clear
How Check Mail Config Data Changed or Not?
Add dd(config('mail'));
in the controller method and navigate the route for this method. Then you can see the output as the following image.
Sample Code To Check Laravel Email Log
Add the following code in the route web.php file or controller method. Then navigate the URL to send an email. Example URL: http://127.0.0.1:8000/send-email
Finally, You can check data from storage/logs/laravel.log file.
use Illuminate\Support\Facades\Mail;
Route::get('send-email', function(){
Mail::send([], [], function ($compose) {
$compose->to('recipient@example.com')
->subject('MailMug Contact')
->from('from@mail.com')
->setBody('Hi, welcome user!');
});
});
Disadvantages of Laravel Email Log
- Difficult to test email data
- HTML preview is not possible
- All data is saved to the .log file
How To Easily Debug Emails From Laravel?
Use an SMTP sandbox account like MailMug.net. It provides a free SMTP sandbox account. It is capturing SMTP traffic from your local or online server. You can analyze email body text/html and email header data.